Python/Django “BadStatusLine” error

前端 未结 3 508
暗喜
暗喜 2020-12-15 20:35

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

相关标签:
3条回答
  • 2020-12-15 21:18

    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.

    0 讨论(0)
  • 2020-12-15 21:31

    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.

    0 讨论(0)
  • 2020-12-15 21:32

    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
    
    try:
        page = urllib2.urlopen(url)
        # do something with page
    except BadStatusLine:
        print "could not fetch %s" % url
    
    0 讨论(0)
提交回复
热议问题