gpt4 book ai didi

python - 我在 VM 环境中使用 Python urllib2 POST 做错了什么?

转载 作者:太空宇宙 更新时间:2023-11-03 19:22:17 27 4
gpt4 key购买 nike

import urllib
import urllib2
from urllib2 import Request

url = 'https://www.google.com'
response = urllib2.urlopen(url)

login_url = (url + '/login')
data = {'email': 'john',
'password': 'example'}
req = Request(login_url, urllib.urlencode(data))
response = urllib2.urlopen(req)


File "test.py", line 50, in <module>
response = urllib2.urlopen(req)
File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 400, in open
response = meth(req, response)
File "/usr/lib/python2.7/urllib2.py", line 513, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.7/urllib2.py", line 438, in error
return self._call_chain(*args)
File "/usr/lib/python2.7/urllib2.py", line 372, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 521, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 405: Method Not Allowed

我正在执行 POST 方法。该脚本在运行 python 2.7.1 的 MAC OSX 上运行,但是在运行 python 2.7 的虚拟机上它会抛出该错误。发出 POST 请求时是否需要传递一些 HTTPHeaders?

最佳答案

您应该在这里找到您需要的文档:

http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html

要进行身份验证,您需要使用此网址 https://www.google.com/accounts/ClientLogin需要您尝试访问的服务,它将为您提供指定服务的 access_token

您需要为此类请求存储 cookie:

import urllib2
import cookielib
cookiejar = cookielib.LWPCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
urllib2.install_opener(opener)
return urllib2

关于python - 我在 VM 环境中使用 Python urllib2 POST 做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9303419/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com