400 Bad Request calling urllib2.urlopen()

A

Achim Domma

Hi,

I have an url which redirects the client to another url. If I paste
the url into a browser, it works fine. If I open it via
urllib2.urlopen() I get the following error:

Traceback (most recent call last):
File "...\crawlRawDocuments.py", line 16, in <module>
body = urlopen(url).read()
File "C:\Python25\lib\urllib2.py", line 121, in urlopen
return _opener.open(url, data)
File "C:\Python25\lib\urllib2.py", line 380, in open
response = meth(req, response)
File "C:\Python25\lib\urllib2.py", line 491, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python25\lib\urllib2.py", line 412, in error
result = self._call_chain(*args)
File "C:\Python25\lib\urllib2.py", line 353, in _call_chain
result = func(*args)
File "C:\Python25\lib\urllib2.py", line 575, in http_error_302
return self.parent.open(new)
File "C:\Python25\lib\urllib2.py", line 380, in open
response = meth(req, response)
File "C:\Python25\lib\urllib2.py", line 491, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python25\lib\urllib2.py", line 418, in error
return self._call_chain(*args)
File "C:\Python25\lib\urllib2.py", line 353, in _call_chain
result = func(*args)
File "C:\Python25\lib\urllib2.py", line 499, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 400: Bad Request

I'm using Python 2.5.1 and Google did not help. Any hint on how to fix
or debug this problem?

regards,
Achim
 
B

Bjoern Schliessmann

Achim said:
urllib2.HTTPError: HTTP Error 400: Bad Request

I'm using Python 2.5.1 and Google did not help. Any hint on how to
fix or debug this problem?

Use e. g. wireshark to look at the raw stream data and check if the
request is malformed.

Regards,


Björn
 
O

O.R.Senthil Kumaran

I have an url which redirects the client to another url. If I paste
the url into a browser, it works fine. If I open it via

Any redirection is setup using either of the 301, 302, 303, 307 return codes of HTTP. Atleast, thats what standard determines.
File "C:\Python25\lib\urllib2.py", line 575, in http_error_302
return self.parent.open(new)

Here we see that redirect handler http_error_302 has taken effect.
File "C:\Python25\lib\urllib2.py", line 499, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 400: Bad Request

The redirected page is not getting fetched by the OpenerDirector of urllib2. This could be due to various reasons.
- the protocol is unsupported.
- there is some prevention from accessing the resource directly.

If its possible, can you share the url which is throwing this error?
To me, it does seem urllib2 issue, it could be the (improper ??) redirection setup at webserver.

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top