python的HTTP操作
今天戴总叫我帮她弄个网页API的测试脚本,用python实现起来相当容易,以下代码是调试通过的,有cookie,post, 响应的实现。
imp class HTTPRefererProcessor(urllib2.BaseHandler): def __init__(self): self.referer = None def http_request(self, request): if ((self.referer is not None) and not request.has_header("Referer")): request.add_unredirected_header("Referer", self.referer) return request def http_response(self, request, response): self.referer = response.geturl() return response https_request = http_request https_response = http_response cj = cookielib.CookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj), HTTPRefererProcessor(),) urllib2.install_opener(opener) da request = urllib2.Request( url = 'http://203.117.16.171:8080/webgamecode/webcore?act headers = {'Content-Type': 'application/x-www-form-urlencoded'}, da ret = opener.open(request) content = ret.read() print content |
posted on 2009-07-14 15:28 Eric Xiang 阅读(213) 评论(0) 编辑 收藏 举报