Python 2.7 在python中,curl响应是通过libcurl2无法读取的字符
场景是在使用此Python 2.7 在python中,curl响应是通过libcurl2无法读取的字符,python-2.7,Python 2.7,场景是在使用此 $curl --compress -ku user https://exampledomain.com/api/v1/alerts curl的输出是压缩的,所以我必须使用--compress选项以可读的格式进行转换 我正试图通过urllib2使用python实现同样的功能 这是密码 import os import getpass import urllib2,ssl import base64 import sys user = os.environ['ID'] passw
$curl --compress -ku user https://exampledomain.com/api/v1/alerts
curl的输出是压缩的,所以我必须使用--compress选项以可读的格式进行转换
我正试图通过urllib2使用python实现同样的功能
这是密码
import os
import getpass
import urllib2,ssl
import base64
import sys
user = os.environ['ID']
password = getpass.getpass('Enter admin password : ')
if (user and password):
url = "https://example.com/api/v1/alerts"
request = urllib2.Request(url)
base64string = base64.encodestring('%s:%s' % (user, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
context = ssl._create_unverified_context()
resp = urllib2.urlopen(request,context=context)
print resp.read()
else:
作为回报,它只是在屏幕上打印不可读的字符。
问题是如何通过urllib2通过python代码转换curl的压缩输出返回
非常感谢您的帮助。谢谢查看模块。@glibdud据我所知,Zipfile与系统上的zip文件有关,而不是与返回content encoding=“zip”的curl有关。您在说明中指出“curl的输出是压缩的”
--compressed
是在curl中处理它的方式,zipfile模块是在python中处理它的方式。@glibdud做一些澄清。当我使用curl而不使用--compress
时,输出是不可读的字符,这与上面提到的当前代码是一样的。当我在curl中使用--compress
时,它给了我想要的结果,所以我希望如何通过上面的代码实现这一点。解决了我的问题。