您如何查看机械化使用的请求标头? [英] How do you view the request headers that mechanize is using?

查看:64
本文介绍了您如何查看机械化使用的请求标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式向表单提交一些数据.我遇到一个小问题,即服务器不喜欢"我发送的内容.令人沮丧的是,没有错误消息,或者任何可以帮助诊断问题的东西,它所做的一切只是让我回到击中br.submit()时开始的同一页面.

I am attempting to submit some data to a form programatically. I'm having a small issue whereby the server is "not liking" what I'm sending it. Frustratingly, there is no error messages, or anything that could help diagnose the issue, all it does is spit me back to the same page I started on when I hit br.submit().

当我在浏览器中手动单击提交"按钮时,结果页面显示一个小的成功!".信息.通过脚本提交时,不会出现此类消息.此外,实际上没有任何更改发布到服务器.这很奇怪,这是我第一次遇到这种行为.

When I click the submit button manually in the browser, the resulting page shows a small "success!" message. No such message appears when submitting via the script. Additionally, no changes are actually being posted to the server. It's quite strange, and the first time I've encountered this behavior.

在研究Mechanize文档时,它表明在这些奇怪且难以诊断的问题下,最好复制浏览器实际提交的请求标头.

Digging through the Mechanize docs, it suggests that under these strange, hard to diagnose issues, that it's best to copy the request headers that are actually submitted by the browser.

我的问题是,当我呼叫br.submit()时,如何查看请求标头是什么?

My question is, how do I see what the request headers are when I call br.submit()?

location = 'http://ww.mysite.com'

br = mechanize.Browser()
cj = mechanize.LWPCookieJar()
br.set_cookiejar(cj)

username = MY_USER_NAME
password = MY_PASSWORD
br.addheaders.append(('Authorization', 'Basic %s' % base64.encodestring('%s:%s' % (username, password))))

br.open(location)

br.select_form(nr=0)
br['text'] = 'MY JUNK TO SUBMIT'    #Text field. Can put anything
br['DropDown1'] = ['4']             #This is a dropdown of integer values
br['DropDown2'] = ['3']             #Also a dropdown of ints
br.submit()

提交表单时,如何查看正在发送哪些标题?

How do I see which headers are being sent when I submit the form?

推荐答案

您是否在问如何查看浏览器或机械化发送的标题?

Are you asking how to see what headers your browser or mechanize is sending?

浏览器

像其他评论员一样,您可以使用开发人员 Firebug (Firefox)的插件检查浏览器发送的标头工具(即'F12',Chrome 开发人员工具和Opera

Like the other commentators say you can check the headers sent by the browsers with a plugin like Firebug (Firefox), Developer tools (IE 'F12', Chrome Developer tools and Opera Dragonfly) etc.

机械化

通过机械化,您可以通过执行类似

With mechanize you can get a copy of the headers sent by doing something like

import mechanize 

br = mechanize.Browser()
br.open("http://stackoverflow.com")
request = br.request
request.header_items()

在这种情况下,哪个给出

Which gives in this case

[('Host', 'stackoverflow.com'), ('User-agent', 'Python-urllib/2.7')]


其他/一次性

一如既往的一次性调试或如果未提供任何内容,则可以使用 Wireshark 检查哪些标头是已通过网络发送. 提示:使用(http.request.uri == "http://stackoverflow.com/")

As always for a one off debug or if nothing is provided then you can use Wireshark to check what headers are been sent over the network. Tip: use a filter like (http.request.uri == "http://stackoverflow.com/")

这篇关于您如何查看机械化使用的请求标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆