Wednesday 15 September 2010

python - Handling urllib2's return of badstatusline(line)? -


I have a simple internet checker but it sometimes gives an error that does not seem to handle me ...

Here is the function:

  def internet_on (): try: urllib2.urlopen ("http://google.co.uk/", timeout = 10) Yaleb 2 as return e This is true except for the horoscope: false return except the socket. E: wrong as the return:   

Here is the error:

  Traceback (most recent call final): File "C: / Testcript Py ", line 117, Internet_on urllib2.urlopen (" http://google.co.uk/ ", timeout = 10) file" C: \ Python27 \ lib \ urllib2.py ", line 127, urlopen return _opener.open (Url, data, timeout) in the file "C: \ Python27 \ lib \ urllib2.py", line 410, open response = meth (request, response) file "C: \ Python27 \ lib \ urllib2.py" http_response ' Http ', request, feedback, code, msg, hdrs) file "C: \ Python27 \ lib \ urllib2.py", on line 442, error The result is "c: \ Python27 \ lib \ urllib2" in the file named "C: \ Python27 \ lib \ urllib2.py", in line 382, ​​_call_chai, in the N result = func (* args) file. Py ", line 629, http_error_302 in self.parent.open (new, timeout = req.timeout) file" C: \ Python27 \ lib \ Urllib2.py ", file in open response = self._open (req, data) "C: \ Python27 \ lib \ urllib2.py", in the row 422, _open '_open', req) the file "C: \" Python27 \ lib \ urllib2.py ", line 382, ​​_call_chain result = func (* Args) in the file "C: \ Python27 \ lib \ urllib2.py", line 1214, http_open return to self.do_open (httplib.HTTPConnection, Req) in "C: \ Python27 \ lib \ urllib2.py", line 1187 Do_open r = h.getresponse (buffer Ing = True) The file "C: \ Python27 \ lib \ httplib.py", line 1045, getresponse Response.begin () file "C: \ Python27 \ lib \ httplib.py", line 409, in the start version, status , Cause = self._read_status () file "C: \ Python27 \ lib \ httplib.py", in line 373, _read_status raise BadStatusLine (line) BadStatusLine: ''   

How do I get wrong I can handle this error to return, I want to correct the Internet_On function if he connects, but if anything should return false to the OT real ..

  import plugin ... def internet_on (): try: urllib2.urlopen ("http://google.co.uk/", timeout = 10) except for return true (IOError, httplib.HTTPException ): Wrong return    

No comments:

Post a Comment