Python 2.7 python 2.7错误:';引发HTTPError(请求获取完整url(),代码,消息,hdrs,fp)HTTPError';在类中使用urlopen时
我创建了以下功能,该功能运行良好:Python 2.7 python 2.7错误:';引发HTTPError(请求获取完整url(),代码,消息,hdrs,fp)HTTPError';在类中使用urlopen时,python-2.7,Python 2.7,我创建了以下功能,该功能运行良好: def executeMethod(inJson): global FunctionCalls global Logger FunctionCalls.append("executeMethod") print(inJson) print("") url = API_URL headers = {"Content-Type":"application/json"} data = json.dumps(inJson) prin
def executeMethod(inJson):
global FunctionCalls
global Logger
FunctionCalls.append("executeMethod")
print(inJson)
print("")
url = API_URL
headers = {"Content-Type":"application/json"}
data = json.dumps(inJson)
print (data)
print("")
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req).read()
print (json.loads(response))
try:
return json.loads(response)["serviceId"]
except:
return -1
现在我知道它可以工作了,我正在尝试将此代码合并到一个类中:
def executeMethod(self):
self.FunctionCalls.append("executeMethod")
print(self.jsonBody)
print("")
url = self.API_URL
headers = {"Content-Type":"application/json"}
data = json.dumps(self.jsonBody)
print (data)
print("")
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req).read()
print (json.loads(response))
try:
return json.loads(response)["serviceId"]
except:
return -1
但是当我尝试使用这个类函数时,我得到以下错误:
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError
没有HTTP错误代码或任何东西。我已经多次比较了代码,甚至从它工作的函数重新创建了类函数。我不知道还有什么可以尝试。更新,似乎问题出在请求主体,而不是代码。当我传递服务器期望的JSON时,运行正常。然后请删除您的问题,因为它对其他任何人都没有用处。