将此curl命令转换为python请求

将此curl命令转换为python请求,python,curl,cordova,Python,Curl,Cordova,我需要发送一个pythonpost命令,在该命令上我有一个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/app

我需要发送一个pythonpost命令,在该命令上我有一个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)
从curl示例中,我觉得Phonegap需要的是JSON文件,而不是多部分POST字段。您也不应设置标题,将其保留在POST中的文件字段中:

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})

可以发布错误消息吗?我删除了Python post数据;它没有添加任何内容,而且太长了。@SourabhGupta:我只是针对http://httpbin.org/post 同样的信息也被发布了。我还检查了数据键中的JSON,这肯定是他们期望的数据键。@SourabhGupta:如果这对您仍然不起作用,您需要联系Phonegap支持论坛:谢谢Markjin,它现在起作用了。但这在昨天是行不通的。我不知道背后的原因。@SourabhGupta:你最后一个错误是因为一根断了的管子;连接关闭得太早,表示服务器端出现错误。@SourabhGupta:表示PhoneGap或internet连接出现问题,而不是post格式不正确。另外,很高兴能帮上忙,我们确实找到了正确的帖子格式,不是吗?