簡體   English   中英

卷曲有效,請求無效

[英]Curl works, requests doesn't

有人告訴我使用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

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

您可以使用api這樣從特定的提交下載存儲庫。 如果您轉到回購中的“下載”部分,則應該看到一個可以使用的類似於此的鏈接。

response = requests.get('https://bitbucket.org/{TeamName}/{RepoName/get/{sha}.zip', auth=('User', 'Pass'))

這為我工作:

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)

請注意,我不必設置標題或禁用驗證。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM