2

I need it when it is a file it downloads and when it is a directory it accesses the directory, could anyone help me? because when I go to a folder it gets /Python/Python/directory and does not access, like leaving it only /python/directory

Thanks

#!/usr/bin/python

import web
import os
urls = (
    '/(.*)', 'hello'
    )
app = web.application(urls, globals())

class hello:
    def GET(self, path):

        path = '/Python/'+path
        lista = '<html> <body>'
        caminhos = [os.path.join(path, nome) for nome in os.listdir(path)]
        diretorios = [dire for dire in caminhos if os.path.isdir(dire)]
        for dire in diretorios:
#            lista = lista+dire+'<br>'
            lista = lista+'<a href='+dire+'>'+dire+'</a><br>'
        arquivos = [arq for arq in caminhos if os.path.isfile(arq)]
        for arq in arquivos:
            lista = lista+'<a href='+arq+' target="_blank">'+arq+'</a><br>'
#            lista = lista+arq+'<br>'
        lista = lista+'<br><br><a href="javascript:window.history.go(-1)">Voltar</a></body> </html>'
        return lista


if __name__ == "__main__":
    app.run()

1 Answer 1

1

Your def GET() is adding /Python/ to the start of every URL. This works the first time, because you GET /, and it returns you a list from /Python/ directory.

However, when you click on one of the items, say "foo" in the returned HTML, you're doing a GET /Python/foo. Then, again, your code adds another /Python, so it's looking at /Python/Python/foo. One idea is to add '/Python' only if incoming path is '/'.

Replace your first line under GET() with

if path == '':
    path = '/Python/' + path
else:
    path = '/' + path

You'll see some other problems with your current approach, though.

First, consider security. You're granting access to your whole server by allowing a user to request a file. So, make sure your python checks the resulting path is still within your sandbox:

if not os.path.normpath(path).startswith('/Python/'):
    return 'Error'

Second, consider what happens if the user selects a file. You code will attempt:

caminhos = [os.path.join(path, nome) for nome in os.listdir(path)]

which fails, because path is not a directory. So, put all that code within an if statement:

if os.path.isdir(path):
    caminhos = [os.path.join.....
    ...
    lista = lista+'<br><br....

Finally, if someone does select a file, what to do? Put that in your else:

else:
    return open(path, 'r').read()
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but how to apply in my code to not appear for example /Python/Python/foo ? I did not understand :/
Thank you very much, I had not thought about it, you helped me a lot Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.