2014-06-12 37 views
0

我想創建一個命令行工具,使用Python的requests模塊將文件上傳到tempsend.com。如何使用python requests模塊將表單數據變量添加到post請求模塊中?

我已經閱讀了文檔,看起來好像我正在努力完成的任務應該很容易,但是我無法讓它成功發佈文件。

這裏有一些我已經試過格式:

>>> import requests 
>>> url = "http://tempsend.com/send" 
>>> payload = {'file': open('happy.txt', 'rb'), 'expire': '2678400'} 
>>> r = requests.post(url, data=payload) 
>>> r.url 
u'http://tempsend.com/error-nopostdata' 

和:

>>> files = {'file': open('happy.txt', 'rb'), 'expire' : '2678400'} 
>>> r = requests.post(url, files=files) 
>>> r.url 
u'http://tempsend.com/error-badsecondsvalue' 

它看起來像'expire'場不被認可。

我認爲這是一個有效的原始請求應該是什麼樣子:

POST /send HTTP/1.1 
Host: tempsend.com 
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:29.0) Gecko/20100101 Firefox/29.0 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Accept-Language: en-gb,en;q=0.5 
Accept-Encoding: gzip, deflate 
Referer: http://tempsend.com/ 
Cookie: __utma=151760572.1993029721.1402528667.1402603529.1402606574.3;   __utmz=151760572.1402528667.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none);  __utmb=151760572.2.10.1402606574; __utmc=151760572 
Connection: keep-alive 
Content-Type: multipart/form-data;  boundary=---------------------------18953598303296896262036228879 
Content-Length: 835112 

-----------------------------18953598303296896262036228879 
Content-Disposition: form-data; name="file"; filename="modules.alias" 
Content-Type: application/octet-stream 

# Aliases extracted from modules themselves. 
alias aes-asm aes_x86_64 
alias aes aes_x86_64 
alias camellia-asm camellia_x86_64 
<blablablablablablablabla> 


-----------------------------18953598303296896262036228879 
Content-Disposition: form-data; name="expire" 

2678400 
-----------------------------18953598303296896262036228879-- 

回答

1
>>> import requests 
>>> files = {'file': open('happy.txt', 'rb')} 
>>> url = "http://tempsend.com/send" 
>>> r = requests.post(url, data={'expire':'2678400'}, files=files) 
>>> r.url 
u'http://tempsend.com/36DCF220A3' 
>>> 

我找到了答案問這個問題。典型的後5分鐘!