Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python HTTPError:由urllib2.urlopen()永久引发的移动_Python_Python 2.7_Request - Fatal编程技术网

Python HTTPError:由urllib2.urlopen()永久引发的移动

Python HTTPError:由urllib2.urlopen()永久引发的移动,python,python-2.7,request,Python,Python 2.7,Request,我正在尝试使用python 2.7中的urllib和urllib2库向数据库发送修补程序请求(因为我无法使用请求库,因为它在该服务器中不起作用,并且没有人找到解决方案,所以请不要建议请求,因为该路径已关闭) 代码如下所示: data={"name":"whatever name"} data=urllib.urlencode(data,'utf-8')#Encoding the dictionary of the data to make the request req=urllib2.Requ

我正在尝试使用python 2.7中的urllib和urllib2库向数据库发送修补程序请求(因为我无法使用请求库,因为它在该服务器中不起作用,并且没有人找到解决方案,所以请不要建议请求,因为该路径已关闭)

代码如下所示:

data={"name":"whatever name"}
data=urllib.urlencode(data,'utf-8')#Encoding the dictionary of the data to make the request
req=urllib2.Request(url=next_url,headers={"Authorization": auth_header,"Content-Type": "application/json"})#Creating a request object of urllib library
req.add_data=data
req.get_method = lambda: 'PATCH'
resp = urllib2.urlopen(req)
如果不同时分配
req.get\u method=lambda:'PATCH'
req.add\u data=data
,请求类会自动发送一个get请求,该请求有200个响应,因此我猜它与授权和凭据无关。使用Python3和urllib.request库也可以工作,因此服务器可以确保接受补丁请求

我希望任何人都能找到解决办法。。。我无法想象为什么会发生这种情况

更新已解决:我发现问题与我发出请求的url有关。

此“永久移动”错误表示服务器响应HTTP 301错误,表示您请求的url已移动到另一个url()

我建议使用tcpdump或wireshark等工具进行网络流量捕获,以检查HTTP对话并确认。如果服务器实际使用301进行响应,并且这不是urllib引发错误代码,那么服务器响应应该包含一个带有另一个URL的“Location”头,您应该尝试使用此头

请注意,因此您可能需要重新考虑尝试使“requests”模块工作