2013-06-11 21 views
0

有沒有簡單的方法使用Python上傳單個文件?
我知道requests,但它發佈了包含單個文件的文件的字典,所以我們在接收另一端的一個文件時遇到了一些問題。在Python中發佈單個文件

目前代碼發送該文件是:

def sendFileToWebService(filename, subpage): 
    error = None 
    files = {'file': open(filename, 'rb')} 
    try: 
     response = requests.post(WEBSERVICE_IP + subpage, files=files) 
     data = json.load(response) 
(...) 

而問題是,requests發送每個文件中

--7163947ad8b44c91adaddbd22414aff8 
Content-Disposition: form-data; name="file"; filename="filename.txt" 
Content-Type: text/plain 


<beggining of file content> 
(...) 
<end of file content> 
--7163947ad8b44c91adaddbd22414aff8-- 

我想這是一個文件包。有沒有辦法發送文件「清除」?

回答

2

使用data參數的要求,而不是files參數:

def sendFileToWebService(filename, subpage): 
    error = None 
    try: 
     response = requests.post(WEBSERVICE_IP + subpage, 
           data=open(filename, 'rb')) 
     data = json.load(response) 
(...) 

這將導致該文件的內容放置在HTTP請求的主體。指定files參數會觸發請求切換到multipart/form-data

相關問題