python发送HTTP请求,python发送请求,GET 方法>>> im
python发送HTTP请求,python发送请求,GET 方法>>> im
GET 方法
>>> import httplib >>> conn = httplib.HTTPConnection("www.python.org") >>> conn.request("GET", "/index.html") >>> r1 = conn.getresponse() >>> print r1.status, r1.reason 200 OK >>> data1 = r1.read() >>> conn.request("GET", "/parrot.spam") >>> r2 = conn.getresponse() >>> print r2.status, r2.reason 404 Not Found >>> data2 = r2.read() >>> conn.close()
HEAD 方法
>>> import httplib >>> conn = httplib.HTTPConnection("www.python.org") >>> conn.request("HEAD","/index.html") >>> res = conn.getresponse() >>> print res.status, res.reason 200 OK >>> data = res.read() >>> print len(data) 0 >>> data == '' True
POST 方法
>>> import httplib, urllib >>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0}) >>> headers = {"Content-type": "application/x-www-form-urlencoded", ... "Accept": "text/plain"} >>> conn = httplib.HTTPConnection("musi-cal.mojam.com:80") >>> conn.request("POST", "/cgi-bin/query", params, headers) >>> response = conn.getresponse() >>> print response.status, response.reason 200 OK >>> data = response.read() >>> conn.close()
相关内容
- try...finally 异常,try...finally,异常处理import t
- 文件读写,,文件读写#第九章 文件#
- 字典 暗示,字典,[Python]代码#
- 字典,,字典-基础# 字典#
- python Singleton pattern,pythonsingleton,Python singl
- while循环输出(1-10),while循环输出1-10,[Python]代码i
- python读取目录下文件并生成日志,,[Python]代码
- UTF8文件去BOM,UTF8文件BOM,[Python]代码#!
- python使用sha1加密方法验证文件的有效性,,import hashl
- 批量去除UTF8的Bom标签,utf8bom标签,#!/usr/bin/p
评论关闭