简体   繁体   English

在Python中机械化更改HTTP标头的大小写

[英]Mechanize in Python Changes HTTP headers case

I am trying to automate a web page request by using mechanize in python. 我正在尝试通过在python中使用机械化来自动化网页请求。 When I add custom headers like 当我添加自定义标题时
X-Session= 'abc'
and
X-Auth='123'
by using addheader function. 通过使用addheader函数。

object=mechanize.Browser()
object.addheaders=[('X-Session','abc'),('X-Auth','123')]

It changes those headers to X-session and X-auth . 它将那些标头更改为X-sessionX-auth I believe due to that the server is not able to authenticate me. 我相信由于服务器无法对我进行身份验证。 Can anybody help how to maintain the case? 有人可以帮助维持案件吗? Thanks. 谢谢。

Mechanize expect two items tuple as header, first item is header name, second is value, so you must do: 机械化期望将两个元组作为标题,第一个是标题名称,第二个是值,因此您必须执行以下操作:

object.addheaders=[('X-Session','abc'), ('X-Auth','123')]

(Two tuples of two elements instead of one tuple with 4 elements). (两个元组包含两个元素,而不是一个元组包含4个元素)。


To check headers that Mechanize will send with query, you can do: 要检查Mechanize将随查询发送的标头,您可以执行以下操作:

print(request.header_items())

This should print something like: 这应该打印类似:

[('X-Session','abc'), ('X-Auth','123')]

Doc: http://wwwsearch.sourceforge.net/mechanize/doc.html#adding-headers Doc: http : //wwwsearch.sourceforge.net/mechanize/doc.html#adding-headers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM