从 URL 中提取查询字符串

我正在尝试从用户输入的 url 中提取查询字符串,该字符串将变成整数,因此http://127.0.0.1:8000/?1653。当我尝试这样做时,python 给了我错误,


文件“C:\Users...\Documents\webserver\server.py”,第 26 行,在 do_GET int(URIextra) ValueError 中:int() 的无效文字,基数为 10:''


我不知道我在这里做错了什么,任何帮助将不胜感激。


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()


翻翻过去那场雪
浏览 225回答 2
2回答

呼啦一阵风

您正在使用 URIExtra 作为字符串:URIextra = str(o[4])你应该使用这个:URIextra = int(o[4])

三国纷争

我能够更改我的代码以更有效地工作,现在没有我的电脑,稍后将在此处发布
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python