为了账号安全,请及时绑定邮箱和手机立即绑定

从 URL 中提取查询字符串

从 URL 中提取查询字符串

翻翻过去那场雪 2021-08-05 16:16:20
我正在尝试从用户输入的 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, osfrom urllib.parse import urlparsefrom shutil import copyfiledirs = []PORT = 8000URIextra = ""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 = Trueos.chdir(os.getcwd() + "/web")httpd = MyTCPServer(('localhost', PORT), CustomHandler)httpd.allow_reuse_address = Trueprint("Serving at port", PORT)httpd.serve_forever()
查看完整描述

2 回答

?
呼啦一阵风

TA贡献1802条经验 获得超6个赞

您正在使用 URIExtra 作为字符串:

URIextra = str(o[4])

你应该使用这个:

URIextra = int(o[4])


查看完整回答
反对 回复 2021-08-05
?
三国纷争

TA贡献1804条经验 获得超7个赞

我能够更改我的代码以更有效地工作,现在没有我的电脑,稍后将在此处发布


查看完整回答
反对 回复 2021-08-05
  • 2 回答
  • 0 关注
  • 165 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号