2016-09-22 108 views
0

我想在Github的Jquery項目中獲取代碼文件的解密內容。如果我做了curl請求,則返回的代碼內容將被解密。Python請求與Mac上的Curl請求通過Github API獲取代碼內容

但是,在Python請求中使用相同的參數時,存在加密。爲什麼是這樣,我能做些什麼來獲得解密版本?

這裏是我的curl命令:

curl https://api.github.com/repos/jquery/jquery/git/blobs/1d2872e34a809a9469ac5cb149a40fc7b8007633 -H "Accept: application/vnd.github-blob.raw" 

輸出如下:

<?php 
    # Load and run the test suite as a proper XHTML page 
    header("Content-type: application/xhtml+xml"); 
    readfile("index.html"); 
?> 

這裏是我的Python代碼:

import requests 

code = requests.get('https://api.github.com/repos/jquery/jquery/git/blobs/1d2872e34a809a9469ac5cb149a40fc7b8007633'\ 
          ,headers={'content-type':'application/vnd.github-blob.raw'}) 

code.json() 

輸出是這樣的:

{'content': 'PD9waHAKCSMgTG9hZCBhbmQgcnVuIHRoZSB0ZXN0IHN1aXRlIGFzIGEgcHJv\ncGVyIFhIVE1MIHBhZ2UKCWhlYWRlcigiQ29udGVudC10eXBlOiBhcHBsaWNh\ndGlvbi94aHRtbCt4bWwiKTsKCXJlYWRmaWxlKCJpbmRleC5odG1sIik7Cj8+\nCg==\n', 
'encoding': 'base64', 
'sha': '1d2872e34a809a9469ac5cb149a40fc7b8007633', 
'size': 136, 
'url': 'https://api.github.com/repos/jquery/jquery/git/blobs/1d2872e34a809a9469ac5cb149a40fc7b8007633'} 

回答

2
>>> import base64 
>>> base64.b64decode('PD9waHAKCSMgTG9hZCBhbmQgcnVuIHRoZSB0ZXN0IHN1aXRlIGFzIGEgcH 
Jv\ncGVyIFhIVE1MIHBhZ2UKCWhlYWRlcigiQ29udGVudC10eXBlOiBhcHBsaWNh\ndGlvbi94aHRtbC 
t4bWwiKTsKCXJlYWRmaWxlKCJpbmRleC5odG1sIik7Cj8+\nCg==') 
'<?php\n\t# Load and run the test suite as a proper XHTML page\n\theader("Conten 
t-type: application/xhtml+xml");\n\treadfile("index.html");\n?>\n' 
>>> 

或者發送您curl命令使用相同的頭......

>>> requests.get('https://api.github.com/repos/jquery/jquery/git/blobs/1d2872e34 
a809a9469ac5cb149a40fc7b8007633',headers={"Accept": "application/vnd.github-blob 
.raw"}).text 
u'<?php\n\t# Load and run the test suite as a proper XHTML page\n\theader("Conte 
nt-type: application/xhtml+xml");\n\treadfile("index.html");\n?>\n' 
>>> 

通知關鍵是「接受」 ......不是「內涵式」

+0

謝謝Joran爲更正。 – MLhacker