网站可用但总是返回“内部服务器错误”
这段代码看起来是这样的:
url ="http://www.example.com"
for a in range(0,10):
opener = urllib2.build_opener()
urllib2.install_opener(opener)
postdata ="info=123456"+str(a)
urllib2.urlopen(url, postdata)
它的作用是向一个特定的网址发送一些数据(比如 http://www.example.com),但是我总是收到一个错误信息,
Traceback (most recent call last):
File "test.py", line 9, in <module>
urllib2.urlopen(url, postdata)
File "c:\Python26\lib\urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "c:\Python26\lib\urllib2.py", line 397, in open
response = meth(req, response)
File "c:\Python26\lib\urllib2.py", line 510, in http_response
'http', request, response, code, msg, hdrs)
File "c:\Python26\lib\urllib2.py", line 435, in error
return self._call_chain(*args)
File "c:\Python26\lib\urllib2.py", line 369, in _call_chain
result = func(*args)
File "c:\Python26\lib\urllib2.py", line 518, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 500: Internal Server Error
我确定这个网站是正常工作的,那我该怎么解决这个问题呢?任何帮助都非常感谢。
2 个回答
1
有些网站不允许使用urllib发送请求。你可以试着更改一下用户代理(User-Agent)。
2
你说你确定这个网站是正常的,但它却返回了一个错误。为什么不试试在你确认网站正常的同时,使用一个网络记录工具,比如wireshark,然后再运行你的测试程序,看看这两个操作发出的请求是否真的一样。如果不一样,那问题就找到了。
如果一样,那就看看服务器的日志。那里应该有更详细的错误信息。如果这个服务器不是你自己的,可以考虑问一下拥有它的人。