将此curl命令转换为python请求

时间:2014-02-19 10:08:55

标签: python curl cordova

我需要发送一个python post命令,我有一个zip文件,它的授权和数据。这是phonegap curl命令: -

curl -F file=@toDoApp.zip -u abc.abc@abc.com -F 'data={"title":"API V1",App","package":"com.alunny.apiv1","version":"0.1.0","create_method":"file"}'  https://build.phonegap.com/api/v1/apps

Python请求

url = 'https://build.phonegap.com/api/v1/apps/'
files =  {'file': open('toDoApp.zip', 'rb')}
auth = ('abc.abc@abc.com', 'password')
headers = {'content-type': 'application/zip'}

requests.post(url, 
    files=files,
    auth=auth, 
    headers=headers, 
    data = {"create_method": "file", "package": "com.alunny.apiv1", "version": "0.1.0", "title": "API V1 App"})

在这次转变中我得到400错误。

Curl命令的详细模式

* Adding handle: conn: 0xc68e50
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0xc68e50) send_pipe: 1, recv_pipe: 0
* About to connect() to build.phonegap.com port 443 (#0)
*   Trying 107.21.217.168...
* Connected to build.phonegap.com (107.21.217.168) port 443 (#0)
* successfully set certificate verify locations:
*   CAfile: none
  CApath: /etc/ssl/certs
* SSLv3, TLS handshake, Client hello (1):
* SSLv3, TLS handshake, Server hello (2):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Server finished (14):
* SSLv3, TLS handshake, Client key exchange (16):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSL connection using AES256-SHA
* Server certificate:
*    subject: C=US; ST=California; L=San Jose; O=Adobe Systems Incorporated; OU=CTSIO; CN=*.phonegap.com
*    start date: 2013-09-13 00:00:00 GMT
*    expire date: 2014-09-25 23:59:59 GMT
*    subjectAltName: build.phonegap.com matched
*    issuer: C=US; O=VeriSign, Inc.; OU=VeriSign Trust Network; OU=Terms of use at https://www.verisign.com/rpa (c)10; CN=VeriSign Class 3 Secure Server CA - G3
*    SSL certificate verify ok.
* Server auth using Basic with user 'sourabh.gupta@xxxx.com'
> POST /api/v1/apps HTTP/1.1
> Authorization: Basic c291cmFiaC5ndXB0YUBoYXNoZWRpbi5jb206Zm9yZG11c3Rhbmc2NTMx
> User-Agent: curl/7.32.0
> Host: build.phonegap.com
> Accept: */*
> Content-Length: 9598897
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=------------------------f7e9d62d637bfe16
> 
< HTTP/1.1 100 Continue
< HTTP/1.1 201 Created
< Cache-Control: max-age=0, private, must-revalidate
< Content-Type: application/json; charset=utf-8
< Date: Wed, 19 Feb 2014 11:27:54 GMT
< ETag: "0ec86d7358720be6e5b9292df9e15557"
* Server Apache/2.2.14 (Ubuntu) is not blacklisted
< Server: Apache/2.2.14 (Ubuntu)
< Set-Cookie: _okapi_session=BAh7ByIPc2Vzc2lvbl9pZCIlMmQ0YjE2MzJhOGY5N2UyZjAyMmE5YmU2NjQyNTY3MzAiG3dhcmRlbi51c2VyLnBlcnNvbi5rZXlbCCILUGVyc29uWwZpAzikBiIiJDJhJDEwJHA0Z0ZtM0JaL3lKRlRzcFFud1RVSk8%3D--bbb1af78e667dd1c7968c7ad5a18920aed935786; path=/; HttpOnly
< Status: 201
< X-Powered-By: Phusion Passenger (mod_rails/mod_rack) 3.0.7
< X-Rack-Cache: invalidate, pass
< X-Request-Id: c26bd35d95a4f4a87f5c91ccf35e50b8
< X-Runtime: 12.509392
< X-UA-Compatible: IE=Edge,chrome=1
< Content-Length: 789
< Connection: keep-alive
< 
* Connection #0 to host build.phonegap.com left intact
{"build_count":null,"status":{"android":"pending","blackberry":"skip","webos":"skip","ios":null,"symbian":"skip","winphone":"pending"},"phonegap_version":"3.1.0","description":"Hello World sample application that responds to the deviceready event.","link":"/api/v1/apps/786192","title":"HelloWorld","repo":null,"debug":false,"hydrates":false,"package":"com.phonegap.helloworld","keys":{"android":null,"blackberry":null,"ios":null},"private":true,"error":{},"collaborators":{"link":"/api/v1/apps/786192/collaborators","pending":[],"active":[{"link":"/api/v1/apps/786192/collaborators/763964","person":"sourabh.gupta@xxxx.com","id":763964,"role":"admin"}]},"version":"1.0.0","icon":{"link":"/api/v1/apps/786192/icon","filename":"icon-72-2x.png"},"id":786192,"download":{},"role":"admin"}

例外:

Traceback (most recent call last):
  File "test.py", line 21, in <module>
    requests.post(url, files=files, auth=auth, data={'data': json_data})
  File "/home/sattva/virtual_env/vedas/local/lib/python2.7/site-packages/requests/api.py", line 88, in post
    return request('post', url, data=data, **kwargs)
  File "/home/sattva/virtual_env/vedas/local/lib/python2.7/site-packages/requests/api.py", line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/sattva/virtual_env/vedas/local/lib/python2.7/site-packages/requests/sessions.py", line 383, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/sattva/virtual_env/vedas/local/lib/python2.7/site-packages/requests/sessions.py", line 486, in send
    r = adapter.send(request, **kwargs)
  File "/home/sattva/virtual_env/vedas/local/lib/python2.7/site-packages/requests/adapters.py", line 378, in send
    raise ConnectionError(e)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='build.phonegap.com', port=443): Max retries exceeded with url: /api/v1/   apps/ (Caused by <class 'socket.error'>: [Errno 32] Broken pipe)

1 个答案:

答案 0 :(得分:2)

从您的curl示例中,我看起来像Phonegap期待的是JSON文件,而不是多部分POST字段。您也不应该设置标题,将其保留在POST中的file字段:

import json

url = 'https://build.phonegap.com/api/v1/apps/'
json_data = json.dumps({
    "create_method": "file", 
    "package": "com.alunny.apiv1",
    "version": "0.1.0", 
    "title": "API V1 App"})
files =  {'file': open('toDoApp.zip', 'rb')}
auth = ('abc.abc@abc.com', 'password')
requests.post(url, files=files, auth=auth, data={'data': json_data})