Mac终端上的Python请求与卷曲请求通过Github API获取代码内容

时间:2016-09-22 18:39:42

标签: python curl github

我想在Github上的Jquery项目中获取代码文件的解密内容。如果我做curl请求,则返回的代码内容将被解密。

但是在Python请求中使用相同的参数,加密存在。为什么这样,我能做些什么来获得解密版本?

这是我的卷曲命令:

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

1 个答案:

答案 0 :(得分: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'
>>>

注意密钥是&#34;接受&#34; ......不是&#34;内容类型&#34;