我被告知使用curl是不好的,你永远不应该找到python方式来做到这一点。请求为王。我的问题是,如何在请求中找到一种方法来执行以下命令,因为我很难过。
Curl在git bash中完美运行。问题是当我想使用python时...请求对我不起作用。见下文
curl -u "USER:PASS" -L -O https://api.bitbucket.org/2.0/repositories/EEEE/AAA/downloads/zip.zip
但我怎么能用请求模仿这个。可以这样做吗?
import requests
from requests.auth import HTTPBasicAuth
url = "https://bitbucket.org/SYA/Music/downloads/SSR131.zip"
src = r"C:\1\1\SR131.zip"
headers = {'Content-Type': 'application/json'}
respo = requests.get(url, stream=True, verify=False, auth=('##############', '######################'), headers=headers)
if respo.status_code == requests.codes.ok:
out = open(src, "wb")
for block in respo.iter_content(1024):
if not block:
break
out.write(block)
out.close()
else:
print("Not able to download ZIP url {url}: {status}, {content}".format(url=url, status=respo.status_code, content=respo.content))
错误:
无法下载ZIP网址https ......:401,b''
更具体地说:
请求的调试输出:
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.bitbucket.org
C:\Users\Imapotatofish\AppData\Local\Continuum\miniconda3\lib\site-packages\urllib3\connectionpool.py:858: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
send: b'GET /2.0/repositories/Imapotatofish/EEE/downloads/zip.zip HTTP/1.1\r\nHost: api.bitbucket.org\r\nUser-Agent: python-requests/2.18.4\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nConnection: keep-alive\r\nAuthorization: Basic VHlzb25kb2dlcnpAZ21haWwuY29tOlJLRDJSUlRXMnhVNnhjWGh6Y3lY\r\n\r\n'
InsecureRequestWarning) repositories/EEEE/AAA/downloads/zip.zi
DEBUG:urllib3.connectionpool:https://api.bitbucket.org:443 "GET /2.0/repositories/Imapotatofish/EEE/downloads/zip HTTP/1.1" 401 0
reply: 'HTTP/1.1 401 Unauthorized\r\n'
header: Server header: WWW-Authenticate header: Content-Type header: Strict-Transport-Security header: Date header: X-Served-By header: X-Static-Version header: ETag header: X-Render-Time header: Connection header: X-Version header: X-Request-Count header: X-Frame-Options header: Content-Length Not able to download ZIP url https://api.bitbucket.org/2.0/repositories/Imapotatofish/EEE/downloads/zip: 401, b''
调试git bash curl
卷曲-v -I -H“测试:测试标题,以便您看到此作品”-u“USER:PASS”-L -O https://api.bitbucket.org/2.0....zip.zip
* Connected to api.bitbucket.org (2401:1d80:1010::153) port 443 (#0)
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* ALPN, offering h2
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
* CAfile: C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt
CApath: none
* TLSv1.2 (OUT), TLS header, Certificate Status (22):
} [5 bytes data]
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
} [512 bytes data]
* TLSv1.2 (IN), TLS handshake, Server hello (2):
{ [100 bytes data]
* TLSv1.2 (IN), TLS handshake, Certificate (11):
{ [2417 bytes data]
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
{ [149 bytes data]
* TLSv1.2 (IN), TLS handshake, Server finished (14):
{ [4 bytes data]
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
} [70 bytes data]
* TLSv1.2 (OUT), TLS change cipher, Client hello (1):
} [1 bytes data]
* TLSv1.2 (OUT), TLS handshake, Finished (20):
} [16 bytes data]
* TLSv1.2 (IN), TLS change cipher, Client hello (1):
{ [1 bytes data]
* TLSv1.2 (IN), TLS handshake, Finished (20):
{ [16 bytes data]
* SSL connection using TLSv1.2 / ECDHE-ECDSA-AES128-GCM-SHA256
* ALPN, server accepted to use h2
* Server certificate:
* subject: C=US; ST=California; L=San Francisco; O=Atlassian, Inc.; OU=Bitbucket; CN=*.bitbucket.org
* start date: Jun 12 00:00:00 2017 GMT
* expire date: Jun 16 12:00:00 2020 GMT
* subjectAltName: host "api.bitbucket.org" matched cert's "*.bitbucket.org"
* issuer: C=US; O=DigiCert Inc; OU=www.digicert.com; CN=DigiCert SHA2 High Assurance Server CA
* SSL certificate verify ok.
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
} [5 bytes data]
* Server auth using Basic with user 'Imapotatofish'
* Using Stream ID: 1 (easy handle 0x267f970)
} [5 bytes data]
> HEAD /2.0/repositories/Imapotatofish/EEE/downloads/zip.zip HTTP/2
> Host: api.bitbucket.org
> Authorization: Basic VHlzb25kb2dlcno6UktEMlJSVFcyeFU2eGNYaHpjeVg=
> User-Agent: curl/7.57.0
> Accept: */*
> Testing: Test header so you see this works
>
{ [5 bytes data]
* Connection state changed (MAX_CONCURRENT_STREAMS updated)!
} [5 bytes data]
< HTTP/2 302
< server: nginx
< vary: Authorization
< content-type: text/html; charset=utf-8
< x-oauth-scopes: pipeline, snippet, issue, pullrequest, project, team, account
< strict-transport-security: max-age=31536000; includeSubDomains; preload
< date: Fri, 12 Jan 2018 15:53:00 GMT
< location: https://bbuseruploads.s3.amazonaws.com/b7f9d00a-c3b1-4451-81d6-157d03db2935/downloads/cc27ac18-7a6e-4a39-b329-23afd4200b65 /zip.zip?Signature=ohS6hkNDuO5UcxaWCWJQJUfmeHI%3D&Expires=1515773632&AWSAccessKeyId=AKIAIQWXW6WLXMB5QZAQ&versionId=0eLFTVqqjU xvLXwicZ9PEuLkiL38CPSI&response-content-disposition=attachment%3B%20filename%3D%22zip.zip%22
< x-served-by: app-162
< etag: "d41d8cd98f00b204e9800998ecf8427e"
< x-static-version: e7f50eb4e170
< x-content-type-options: nosniff
< x-accepted-oauth-scopes: repository
< x-credential-type: apppassword
< x-render-time: 0.0570769309998
< x-request-count: 92
< x-frame-options: SAMEORIGIN
< x-version: e7f50eb4e170
< content-length: 0
<
0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0
* Connection #0 to host api.bitbucket.org left intact
* Issue another request to this URL: 'https://bbuseruploads.s3.amazonaws.com/b7f9d00a-c3b1-4451-81d6-157d03db2935/downloads/cc27ac18-7 a6e-4a39-b329-23afd4200b65/zip.zip?Signature=ohS6hkNDuO5UcxaWCWJQJUfmeHI%3D&Expires=1515773632&AWSAccessKeyId=AKIAIQWXW6WLXMB 5QZAQ&versionId=0eLFTVqqjUxvLXwicZ9PEuLkiL38CPSI&response-content-disposition=attachment%3B%20filename%3D%22zip.zip%22'
0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0* Trying 52.216.130.19...
* TCP_NODELAY set
* Connected to bbuseruploads.s3.amazonaws.com (52.216.130.19) port 443 (#1)
* ALPN, offering h2
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
* CAfile: C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt
CApath: none
* TLSv1.2 (OUT), TLS header, Certificate Status (22):
} [5 bytes data]
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
} [512 bytes data]
* TLSv1.2 (IN), TLS handshake, Server hello (2):
{ [87 bytes data]
* TLSv1.2 (IN), TLS handshake, Certificate (11):
{ [2514 bytes data]
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
{ [333 bytes data]
* TLSv1.2 (IN), TLS handshake, Server finished (14):
{ [4 bytes data]
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
} [70 bytes data]
* TLSv1.2 (OUT), TLS change cipher, Client hello (1):
} [1 bytes data]
* TLSv1.2 (OUT), TLS handshake, Finished (20):
} [16 bytes data]
* TLSv1.2 (IN), TLS change cipher, Client hello (1):
{ [1 bytes data]
* TLSv1.2 (IN), TLS handshake, Finished (20):
{ [16 bytes data]
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-SHA
* ALPN, server did not agree to a protocol
* Server certificate:
* subject: C=US; ST=Washington; L=Seattle; O=Amazon.com Inc.; CN=*.s3.amazonaws.com
* start date: Sep 22 00:00:00 2017 GMT
* expire date: Jan 3 12:00:00 2019 GMT
* subjectAltName: host "bbuseruploads.s3.amazonaws.com" matched cert's "*.s3.amazonaws.com"
* issuer: C=US; O=DigiCert Inc; OU=www.digicert.com; CN=DigiCert Baltimore CA-2 G2
* SSL certificate verify ok.
} [5 bytes data]
> HEAD /b7f9d00a-c3b1-4451-81d6-157d03db2935/downloads/cc27ac18-7a6e-4a39-b329-23afd4200b65/zip.zip?Signature=ohS6hkNDuO5Ucxa WCWJQJUfmeHI%3D&Expires=1515773632&AWSAccessKeyId=AKIAIQWXW6WLXMB5QZAQ&versionId=0eLFTVqqjUxvLXwicZ9PEuLkiL38CPSI&response-content-dis position=attachment%3B%20filename%3D%22zip.zip%22 HTTP/1.1
> Host: bbuseruploads.s3.amazonaws.com
> User-Agent: curl/7.57.0
> Accept: */*
> Testing: Test header so you see this works
>
{ [5 bytes data]
< HTTP/1.1 403 Forbidden
< x-amz-request-id: 63280EEF1A865128
< x-amz-id-2: Ui+My1yzmtT+yjmRjg8eASUW2lUPf1EQUDu08hSXSsYTgPM4RPS9oJ1V0XEen/XodyfJz9aeG4w=
< Content-Type: application/xml
< Transfer-Encoding: chunked
< Date: Fri, 12 Jan 2018 15:53:01 GMT
< Server: AmazonS3
<
0 0 0 0 0 0 0 0 --:--:-- 0:00:02 --:--:-- 0
* Connection #1 to host bbuseruploads.s3.amazonaws.com left intact
答案 0 :(得分:0)
你可以使用api从这样的特定提交下载repo。如果你进入你的仓库的下载部分,你会看到一个类似于你可以使用的链接。
response = requests.get('https://bitbucket.org/{TeamName}/{RepoName/get/{sha}.zip', auth=('User', 'Pass'))
答案 1 :(得分:0)
这对我有用:
import requests
url = 'https://bitbucket.org/UserName/repository/downloads/example.zip'
username = 'user@host.tld'
password = 'secret'
result = requests.get(url, stream=True, auth=(username, password))
if not result.status_code == requests.codes.ok:
print "ERROR: Could not get %s" % url
else:
with open('example.zip', 'wb') as zip_file:
for block in result.iter_content(1024):
zip_file.write(block)
请注意,我没有设置标头或禁用验证。