使用 python、HTTP/1.1 和自定义用户代理发布表单数据

发布于 2024-09-26 00:40:56 字数 587 浏览 4 评论 0原文

我有一个需要将数据发布到的表单,但是它必须具有特定的用户代理字符串和 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

输什么也不输骨气 2024-10-03 00:40:56

cURL 允许更多的定制——它可以通过 pycurl 包用于 Python。也许类似下面的内容可以满足您的需求:

import pycurl
import StringIO

response = StringIO.StringIO()
curl = pycurl.Curl()
curl.setopt(pycurl.HTTP_VERSION, pycurl.CURL_HTTP_VERSION_1_1)
curl.setopt(pycurl.USERAGENT, 'Mozilla/5.0 [...]')

curl.setopt(pycurl.WRITEFUNCTION, response.write)
curl.setopt(pycurl.URL, 'http://path.to/form')
curl.setopt(pycurl.POST, 1) 
curl.setopt(pycurl.POSTFIELDS, 'form input') 

curl.perform()
print response.getvalue()

有一个可配置选项列表 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:

import pycurl
import StringIO

response = StringIO.StringIO()
curl = pycurl.Curl()
curl.setopt(pycurl.HTTP_VERSION, pycurl.CURL_HTTP_VERSION_1_1)
curl.setopt(pycurl.USERAGENT, 'Mozilla/5.0 [...]')

curl.setopt(pycurl.WRITEFUNCTION, response.write)
curl.setopt(pycurl.URL, 'http://path.to/form')
curl.setopt(pycurl.POST, 1) 
curl.setopt(pycurl.POSTFIELDS, 'form input') 

curl.perform()
print response.getvalue()

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文