添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm getting a weird error that I can't seem to find a solution for.

This error does not occur every time I hit this segment of code, and neither does it happen for the same iteration through the loop (it happens in a loop). If I run it enough, it doesn't seem to encounter the error and the program executes successfully. Regardless, I'd still like to figure out why this is happening.

Here is my error, versions, trace, etc: http://dpaste.com/681658/

It seems to happen with the following line in my code:

page = urllib2.urlopen(url)

Where url is.... a URL obviously.

And do have import urllib2 in my code.

The BadStatusLine exception is raised when you call urllib2.urlopen(url) and the remote server responds with a status code that python cannot understand.

Assuming that you don't control url, you can't prevent this from happening. All you can do is catch the exception, and manage it gracefully.

from httplib import BadStatusLine
    page = urllib2.urlopen(url)
    # do something with page
except BadStatusLine:
    print "could not fetch %s" % url

Explanations from other users are right and good, but in practice you may find this useful:
In my experience this usually happens when you are sending unquoted values to the url parameters, like values containing spaces or other characters that need to be quotes or url encoded.

Actually, what happens when you send values without proper URL-encoding is that you get a 400 bad request. – andres.riancho May 13, 2013 at 18:36 Maybe the server is set up wrong (like the one I wrote myself) and instead of 400 it sends back nothing. That's a BadStatusLine. – Noumenon Jan 29, 2016 at 2:51 I found sending some special characters within the python headers triggered a BadStatusLine response with some servers. (specifically backslashes and parentheses) – Luke Rehmann Jul 20, 2022 at 0:04

This doesn't have anything to do with Django, it's an exception thrown by urllib2 which couldn't parse the response after fetching your url. It may be a network issue, a malformed response… Some servers / applications throw this kind of error randomly. If you don't control what this URL returns you're left with catching the exception, debugging which URLs are causing problems and trying to identify a pattern.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.