关于 urllib2 和 POST 调用有很多内容,但我遇到了一个问题.
There's a lot of stuff out there on urllib2 and POST calls, but I'm stuck on a problem.
我正在尝试对服务进行简单的 POST 调用:
I'm trying to do a simple POST call to a service:
url = 'myserver/post_service' data = urllib.urlencode({'name' : 'joe', 'age' : '10'}) content = urllib2.urlopen(url=url, data=data).read() print content我可以看到服务器日志,当我发送数据时,它说我正在执行 GET 调用urlopen 的参数.
I can see the server logs and it says that I'm doing GET calls, when I'm sending the data argument to urlopen.
该库引发了 404 错误(未找到),这对于 GET 调用是正确的,POST 调用处理得很好(我也在尝试在 HTML 表单中使用 POST).
The library is raising an 404 error (not found), which is correct for a GET call, POST calls are processed well (I'm also trying with a POST within a HTML form).
推荐答案这可能之前已经回答过:Python URLLib/URLLib2 POST.
This may have been answered before: Python URLLib / URLLib2 POST.
您的服务器可能正在执行从 myserver/post_service 到 myserver/post_service/ 的 302 重定向.执行 302 重定向后,请求会从 POST 更改为 GET(请参阅问题 1401).尝试将 url 更改为 myserver/post_service/.
Your server is likely performing a 302 redirect from myserver/post_service to myserver/post_service/. When the 302 redirect is performed, the request changes from POST to GET (see Issue 1401). Try changing url to myserver/post_service/.
更多推荐
使用 urllib2 进行 POST 调用而不是 GET
发布评论