python - 使用 Mechanize python 提交多部分/表单数据?

标签 python post http-headers http-post mechanize

我正在尝试使用 Mechanize 对 multipart/form-data 发出 POST 请求,这是我实际发布帖子时从 firefox live http header 中的样子:

http://example.com/new/example

POST /new/example HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:13.0) Gecko/20100101 Firefox/13.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Referer: http://example.com/new/example
Cookie: tmgioct=c32MbAGn1sTuZrH8etPqVNU5; __qca=P0-495598852-1339139301054; __utma=189990958.911848588.1339139302.1339556345.1339561805.32; __utmz=189990958.1339139302.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); logged_in=1; tog_appearance_fieldset=fieldset_open; __utmc=189990958; pfu=42375294; pfp=h2YrFoaTr5LtrVys8PMmKNdyuoeA9FNLakxGzrJK; pfe=1371048319; __utmb=189990958.5.10.1339561805
Content-Type: multipart/form-data; boundary=---------------------------41184676334
Content-Length: 2947
-----------------------------41184676334
Content-Disposition: form-data; name="UPLOAD_IDENTIFIER"

0ad3af1c502c7cb59577b01720ee58ff014810c4
-----------------------------41184676334
Content-Disposition: form-data; name="post[state]"

2
-----------------------------41184676334
blahblahblahblah....

-----------------------------41184676334--

这是我的代码:
browser = mechanize.Browser()

url = "http://example.com/new/example"
header = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 5.1; rv:13.0) Gecko/20100101 Firefox/13.0', 
    'Referer': 'http://example.com/new/example',
    'Content-Type': 'multipart/form-data; boundary=---------------------------41184676334'
}

data = "-----------------------------41184676334\rContent-Disposition: form-data; name="UPLOAD_IDENTIFIER"\r\r0ad3af1c502c7cb59577b01720ee58ff014810c4\r-----------------------------41184676334\rContent-Disposition: form-data; name="post[state]"\r\r2\r-----------------------------41184676334\rblahblahblahblah....\r\r-----------------------------41184676334--\r"

req = urllib2.Request(url, data, header)

response = browser.open(req, timeout = 30)
response.close()

我不知道为什么它不起作用。有人知道吗?请帮帮我。

顺便问一下,它与边界有关吗?我在上面的代码中使用了随机数。

最佳答案

来自 MIME media types RFC 2046 :

The canonical form of any MIME "text" subtype MUST always represent a line break as a CRLF sequence.


您的代码仅使用回车符 ('\r');您还需要添加换行符( \n )。

关于python - 使用 Mechanize python 提交多部分/表单数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11012141/

相关文章:

python - Django 以 10 为基数的 int() 文字无效

python - 在python中读取dat文件显示UnicodeDecodeError(文件<class '_io.TextIOWrapper'>)

python - 在 python 3 中导入 win32com 时遇到问题

jquery - 使用 jQuery 在表单中发布多个输入

node.js - 将数据发布到 odata 服务时,nodejs 中的 CSRF token 验证失败

java - 绘图表面、unlockCanvasAndPost 和 IllegalArgumentException

ios - 尝试使用 Alamofire 4 和 OAuth2 发出 POST 请求时出错 - Swift

javascript - 为什么 express-js 不设置 Content-Type header ?

python - 将txt文件按符号分成列表,python3

javascript - Axios 未在请求中发送自定义 header (可能是 CORS 问题)