get請(qǐng)求方法
def get(self, url, params):
params = urllib.parse.urlencode(eval(params)) # 將參數(shù)轉(zhuǎn)為url編碼字符串
url = 'http://' + self.host + ':' + str(self.port) + url + params
request = urllib.request.Request(url, headers=self.headers)
try:
response = urllib.request.urlopen(request)
response = response.read().decode('utf-8') ## decode函數(shù)對(duì)獲取的字節(jié)數(shù)據(jù)進(jìn)行解碼
json_response = json.loads(response) # 將返回?cái)?shù)據(jù)轉(zhuǎn)為json格式的數(shù)據(jù)
return json_response
except Exception as e:
print('%s' % e)
return {}
post請(qǐng)求方法
def post(self, url, data):
data = json.dumps(eval(data))
data = data.encode('utf-8')
url = 'http://' + self.host + ':' + str(self.port) + url
try:
request = urllib.request.Request(url, headers=self.headers)
response = urllib.request.urlopen(request, data)
response = response.read().decode('utf-8')
json_response = json.loads(response)
return json_response
except Exception as e:
print('%s' % e)
return {}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者