繁体   English   中英

python 使用 urllib2 和 urllib 获取请求问题

[英]python using urllib2 and urllib get request issue

当我使用 curl 获取请求成功,但使用 python 调用获取请求失败,任何人都可以帮忙!

shell script:
curl -X GET -H 'Content-type: application/json' -H 'Authorization: Bearer '$token'' -d '[ { "id":"535985", "language":"EN", "Detailes":{"LibraryId":"KF_2gl9xZYKX7TJi66" }, "KeyID":"SF_cY1tKhYiocNluBB" } ]' 'https://XXXX.com'

获取请求成功。

python Script:
data ={ "id":"535985", "language":"EN", "Detailes":{"LibraryId":"KF_2gl9xZYKX7TJi66" }, "KeyID":"SF_cY1tKhYiocNluBB" }
headers = {'content-type': 'application/json', 'Accept': 'application/json', 'Authorization': 'Bearer %s' % (token)}
proxy = urllib2.ProxyHandler({})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
DataForGet=urllib.urlencode(data)
NewUrl= apilink + "?" + DataForGet
request = urllib2.Request(NewUrl, headers)
response = urllib2.urlopen(request, timeout=300)
message = response.read()

返回消息 TypeError: unhashable type

任何人都知道如何使用 urllib2 和 urllib model 的获取请求。 非常感谢!

我发现我们可以定义这个方法来调用 api 获取数据

class RequestMethod(urllib2.Request,object):
    def __init__(self, url, method=None,data=None, headers={},origin_req_host=None, unverifiable=False):
        self.method = method
        super(RequestMethod,self).__init__(url, data=data,headers=headers,origin_req_host=origin_req_host,unverifiable=unverifiable)
    def get_method(self):
        if self.method != None:
            return self.method
        else:
            return super(RequestMethod,self).get_method()
    def set_method(self,method):
        self.method = method
    def setMethodRequest(url,method):
        return RequestMethod(url,method=method)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM