gpt4 book ai didi

python - 如何通过 urllib2 发送 HTTP/1.0 请求?

转载 作者:太空狗 更新时间:2023-10-29 22:23:09 27 4
gpt4 key购买 nike

好像urllib2默认发送HTTP/1.1请求?

最佳答案

为避免猴子修补 httplib(全局更改),您可以子类化 HTTPConnection 并定义您自己的 http 处理程序:

#!/usr/bin/env python
try:
from httplib import HTTPConnection
from urllib2 import HTTPHandler, build_opener
except ImportError: # Python 3
from http.client import HTTPConnection
from urllib.request import HTTPHandler, build_opener

class HTTP10Connection(HTTPConnection):
_http_vsn = 10
_http_vsn_str = "HTTP/1.0"

class HTTP10Handler(HTTPHandler):
def http_open(self, req):
return self.do_open(HTTP10Connection, req)

opener = build_opener(HTTP10Handler)
print(opener.open('http://stackoverflow.com/q/13656757').read()[:100])

关于python - 如何通过 urllib2 发送 HTTP/1.0 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13656757/

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