使用 python、HTTP/1.1 和自定义用户代理发布表单数据
我有一个需要将数据发布到的表单,但是它必须具有特定的用户代理字符串和 HTTP/1.1 标头(不仅仅是主机,它在 POST 字符串中显式查找 HTTP/1.1。)
我已经尝试过这样做,所以如下:
class AppURLopener(urllib.FancyURLopener):
version = "The User Agent String"
urllib._urlopener = AppURLopener()
def send_data(url, kv)
params = urllib.urlencode(kv)
f = urllib.urlopen(url, params)
data = f.read()
f.close()
但是,这是通过 HTTP/1.0 提交的,带有 Host: 标头。查看 urllib (和 urllib2)的源代码看起来代码路径都实例化了一个 HTTPLib HTTP 类对象,该对象从 1.5 开始被标记为兼容类...有没有一种简单的方法可以让 urllib/URLOpener 使用 HTTPConnection 而不是 HTTP?或者我是否缺少另一个解决方案来获得我需要的正确标头?
I have a form that I need to post data to, however it must have a specific user agent string and HTTP/1.1 headers, (not just host it explicitly looks for HTTP/1.1 in the POST string.)
I've attempted this so far as follow:
class AppURLopener(urllib.FancyURLopener):
version = "The User Agent String"
urllib._urlopener = AppURLopener()
def send_data(url, kv)
params = urllib.urlencode(kv)
f = urllib.urlopen(url, params)
data = f.read()
f.close()
However, this submits via HTTP/1.0 with a Host: header. Looking at the source to urllib (and urllib2) It looks like the codepaths all instantiate a HTTPLib HTTP Class object which is marked as a compatible class from 1.5... Is there an easy way to have urllib/URLOpener use HTTPConnection instead of HTTP? Or is there another solution I'm missing to have the proper headers that I need?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
cURL 允许更多的定制——它可以通过 pycurl 包用于 Python。也许类似下面的内容可以满足您的需求:
有一个可配置选项列表 cURL 网站,其中大部分可以通过上面所示的方式在 pycurl 中访问 (pycurl.OPTION_NAME)。
cURL allows for a greater deal of customization -- it's available for Python via the pycurl package. Perhaps something like the following would serve your needs:
There's a list of configurable options on the cURL website, most of which are accessible in pycurl in the manner shown above (pycurl.OPTION_NAME).