gpt4 book ai didi

Python HTTP 请求 URL 结束斜杠

转载 作者:行者123 更新时间:2023-11-28 18:40:09 25 4
gpt4 key购买 nike

我在 Python 中使用 requests 模块,在 URL 后添加尾部斜杠时似乎遇到了问题。我尝试使用不同的方法(替换、正则表达式)删除它,但它似乎没有任何区别。显然出于隐私原因,一些细节已被替换 :)

url = 'http://example.com:8000'
payload = {}
payload['summary'] = "test"
payload['service'] = 10
payload['template'] = 'XXX11112222'
payload['user_name'] = 'john_smith'
payload['justification'] = 'required'
payload['start_date'] = '1417737600'
payload['end_date'] = '1417759200'
r = requests.post(url, params=payload)
print(r.url)

打印出来的URL是这样的。

http://X.X.X.X:8000/?end_date=1417759200&business_justification=required&service=10&summary=test&template=XXX11112222&user_name=john_smith&start_date=1417737600

然后给我这个回复。

200
400 No requested_service or service Parameter Provided

当我获取该 URL 并将其放入浏览器并删除 '?' 之前的 '/' 时,它工作正常。例如:http://example.com:8000?.... 有效,但是 http://example.com:8000/?... 无效。但我不知道如何在 Python 脚本中删除 URL 的尾部斜杠。

最佳答案

这里是您的服务器出了问题,而不是 requests 库。

HTTP RFC 在 section 3.2.2 中声明:

If the abs_path is not present in the URL, it MUST be given as "/" when used as a Request-URI for a resource

没有路径的URL(如http://X.X.X.X:8000)没有绝对路径部分,所以按照标准设置为/。如果您的服务器在没有给出路径时做出不同的响应,那么它就违反了该标准。

除了将 requests 源代码修补到 remove the default value for the path portion of a URL 之外,没有解决方法。 .这必须在 other locations too 中完成;我没有进行详尽的搜索。

关于Python HTTP 请求 URL 结束斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27248521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com