jeudi 1 novembre 2018

Pulling Query String from URL

I'm trying to pull the query string from a url typed in by the user which would be turned into an interger, so http://127.0.0.1:8000/?1653. When i try to do this python gives me the error,

File "C:\Users...\Documents\webserver\server.py", line 26, in do_GET int(URIextra) ValueError: invalid literal for int() with base 10: ''

I don't know what i'm doing wrong here, any help would be much appreciated.

import http.server, socketserver, os
from urllib.parse import urlparse
from shutil import copyfile

dirs = []

PORT = 8000

URIextra = ""


class CustomHandler(http.server.SimpleHTTPRequestHandler):

    def __init__(self, req, client_addr, server):
        http.server.SimpleHTTPRequestHandler.__init__(self, req, client_addr, server)

    def do_GET(self):
        o = urlparse(self.path)
        URIextra = str(o[4])
        http.server.SimpleHTTPRequestHandler.do_GET(self)
        print(URIextra)
        dirs = os.listdir()
        f = open("index.txt", "w")
        f.write(os.listdir())
        f.close()
        int(URIextra)
        copyfile(dirs[URIextra], "CurrentFile.mp3")

class MyTCPServer(socketserver.ThreadingTCPServer):
    allow_reuse_address = True

os.chdir(os.getcwd() + "/web")

httpd = MyTCPServer(('localhost', PORT), CustomHandler)
httpd.allow_reuse_address = True
print("Serving at port", PORT)
httpd.serve_forever()




Aucun commentaire:

Enregistrer un commentaire