Showing posts with label HTTP. Show all posts
Showing posts with label HTTP. Show all posts

Tuesday, July 26, 2011

几个 http 服务器

python 提供了几个 http 的服务器实现,一个是 BaseHTTPServer,然后有加强版的 SimpleHTTPServer 和 CGIHTTPServer 这么几个。我们往往可以在命令行上通过
$ python -m SimpleHTTPServer 8000
在本地开启一个 http 的服务器。这可以方便调试简单的 HTML 页面。通过 CGI 甚至可以调试复杂一点的 AJAX。

python 另外有一个 django 可以实现一个比较复杂的 HTTP 服务器。后面稍微研究下。

Sunday, February 13, 2011

httplib

这是一个用于获得 http 协议内容获得的库,最常见的用处无外于写 browser 或者 crawler。基本用法就是创建 HTTPConnection 对象,然后 request(使用 GET 或者 POST)获得一个 HTTPResponse 对象,如果其 status == 200 且 reason == 'OK',则可以调用 read 方法获得对应的数据。

下面是一个简单的 snippet:
import httplib
conn = httplib.HTTPConnection( 'www.yahoo.com' )
res = conn.request( 'GET', '/', headers = {'User-Agent': ua, 'Cookie': cookie })
if res.status == 200:
    html = res.read()
    # parse the html
else:
    print res.status, res.reason