2017-09-13 84 views
0

我目前有一個cURL命令可以在我的環境中使用。我需要將其轉換成一個Python 2.7的urllib等同,需要一些幫助將cURL POST轉換爲urllib2而不要求

cURL命令中的問題:

curl -k -v -XPOST -H "Authorization: Bearer $Token" -F [email protected]${local_filename} https://interesting_url.com/ 

我需要同時支持一個頭(H)的能力和形式/文件( -F)並且還沒有成功。

This post includes a discussion on headers,但我一直沒能得到「-F」等價工作

+3

如果您可以使用請求,則可以使用https://curl.trillworks.com/翻譯cURL命令 –

+0

如果我們停留在Python 2.7並且無法使用pip,該怎麼辦? – Numpty

+0

請求在2.7下正常工作,您應該可以通過[下載repo](https://github.com/requests/requests/releases)並運行'python setup.py install'來安裝它。 –

回答

1

隨着python3你可以用requests做到這一點:

import requests 

headers = {'Authorization': 'Bearer $Token'} 

files = [('file', open('${local_filename}', 'rb'))] 

requests.post('https://interesting_url.com/', headers=headers, files=files, verify=False) 

與urllib2的文件上載是一個相當複雜的任務(example)。 所以我建議你requests

龍答案,而無需使用PIP和第三方軟件包

您可以實現自定義類MultiPartForm,然後用它來編碼文件:

import itertools 
import mimetools 
import mimetypes 
from cStringIO import StringIO 
import urllib 
import urllib2 

class MultiPartForm(object): 
    """Accumulate the data to be used when posting a form.""" 

    def __init__(self): 
     self.form_fields = [] 
     self.files = [] 
     self.boundary = mimetools.choose_boundary() 
     return 

    def get_content_type(self): 
     return 'multipart/form-data; boundary=%s' % self.boundary 

    def add_field(self, name, value): 
     """Add a simple field to the form data.""" 
     self.form_fields.append((name, value)) 
     return 

    def add_file(self, fieldname, filename, fileHandle, mimetype=None): 
     """Add a file to be uploaded.""" 
     body = fileHandle.read() 
     if mimetype is None: 
      mimetype = mimetypes.guess_type(filename)[0] or 'application/octet-stream' 
     self.files.append((fieldname, filename, mimetype, body)) 
     return 

    def __str__(self): 
     """Return a string representing the form data, including attached files.""" 
     # Build a list of lists, each containing "lines" of the 
     # request. Each part is separated by a boundary string. 
     # Once the list is built, return a string where each 
     # line is separated by '\r\n'. 
     parts = [] 
     part_boundary = '--' + self.boundary 

     # Add the form fields 
     parts.extend(
      [ part_boundary, 
       'Content-Disposition: form-data; name="%s"' % name, 
       '', 
       value, 
      ] 
      for name, value in self.form_fields 
      ) 

     # Add the files to upload 
     parts.extend(
      [ part_boundary, 
       'Content-Disposition: file; name="%s"; filename="%s"' % \ 
       (field_name, filename), 
       'Content-Type: %s' % content_type, 
       '', 
       body, 
      ] 
      for field_name, filename, content_type, body in self.files 
      ) 

     # Flatten the list and add closing boundary marker, 
     # then return CR+LF separated data 
     flattened = list(itertools.chain(*parts)) 
     flattened.append('--' + self.boundary + '--') 
     flattened.append('') 
     return '\r\n'.join(flattened) 

with open(`local_file.txt`) as f: 
    form = MultiPartForm() 
    form.add_file('file', `local_file`, 
        fileHandle=f) 

    # Build the request 
    request = urllib2.Request('https://interesting_url.com/') 
    request.add_header('Authorization', 'Bearer $Token') 
    body = str(form) 
    request.add_header('Content-type', form.get_content_type()) 
    request.add_header('Content-length', len(body)) 
    request.add_data(body) 

    print 
    print 'OUTGOING DATA:' 
    print request.get_data() 

    print 
    print 'SERVER RESPONSE:' 
    print urllib2.urlopen(request).read() 

The source

+0

如果我們停留在Python 2.7並且不能使用pip,該怎麼辦? – Numpty

+1

@Numpty,請參閱複雜版本的請求的更新答案,而無需安裝額外的軟件包 –

+0

謝謝 - 可能需要我花一天時間來適應此問題並進行測試。你沒有在討論複雜性:| – Numpty

0

嘗試尋找到請求庫。之後,您可以使用pip install requests進行安裝,只需在線搜索一些示例代碼即可。根據您的應用程序,它會有所不同。請求庫允許你做很多事情。有很多文檔。我強烈建議你檢查一下。