Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "result.status_code == 200" in Python mean?

Tags:

python

http

In this little piece of code, what is the fourth line all about?

from google.appengine.api import urlfetch
url = "http://www.google.com/"
result = urlfetch.fetch(url)
if result.status_code == 200:
    doSomethingWithResult(result.content)
like image 577
brilliant Avatar asked Dec 12 '09 03:12

brilliant


3 Answers

It's a HTTP status code, it means "OK" (EG: The server successfully answered the http request).

See a list of them here on wikipedia

like image 120
fyjham Avatar answered Oct 20 '22 07:10

fyjham


Whoever wrote that should have used a constant instead of a magic number. The httplib module has all the http response codes.

E.g.:

>>> import httplib
>>> httplib.OK
200
>>> httplib.NOT_FOUND
404
like image 26
dan-gph Avatar answered Oct 20 '22 08:10

dan-gph


200 is the HTTP status code for "OK", a successful response. (Other codes you may be familiar with are 404 Not Found, 403 Forbidden, and 500 Internal Server Error.)

See RFC 2616 for more information.

like image 6
Wyzard Avatar answered Oct 20 '22 06:10

Wyzard