2014-02-19 114 views
0

我需要发送一个python post命令,在该命令上我有一个zip文件,授权它和数据。这是PhoneGap的curl命令: -将此curl命令转换为python请求

curl -F [email protected] -u [email protected] -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 = ('[email protected]', '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 '[email protected]' 
> 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":"[email protected]","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) 
+0

您可以发布错误消息吗? – Anurag

+0

我删除了Python POST数据;它不会添加任何内容并且太长。 –

回答

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 = ('[email protected]', 'password') 
requests.post(url, files=files, auth=auth, data={'data': json_data}) 
+0

谢谢,但它不是在python方面工作。 –

+0

@SourabhGupta:我只是对'http:// httpbin.org/post'测试了'curl'和'requests',并且发布了相同的信息。我还检查了[Phonegap Build API文档](http://docs.phonegap.com/en/edge/guide_phonegap-build_api_index.md.html#The%20PhoneGap%20Build%20API),这绝对是JSON,他们期望在'数据'键。 –

+0

@SourabhGupta:如果这仍然不适用于您,您需要联系Phonegap支持论坛:http://community.phonegap.com/ –