鉴客 python发送HTTP请求
来源:互联网 发布:会计证网络课程 编辑:程序博客网 时间:2024/05/20 09:08
1. 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()
2. 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
3. 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.clo
使用httplib2
>>> from httplib2 import Http>>> from urllib.parse import urlencode>>> h = Http()>>> data = {"name": "Joe", "comment": "A test comment"}>>> resp, content = h.request("http://bitworking.org/news/223/Meet-Ares", "POST", urlencode(data))
#!/usr/bin/python3import urllib.parseimport httplib2http = httplib2.Http()url = 'http://www.example.com/login' body = {'USERNAME': 'foo', 'PASSWORD': 'bar'}headers = {'Content-type': 'application/x-www-form-urlencoded'}response, content = http.request(url, 'POST', headers=headers, body=urllib.parse.urlencode(body))headers = {'Cookie': response['set-cookie']}url = 'http://www.example.com/home' response, content = http.request(url, 'GET', headers=headers)
0 0
- 鉴客 python发送HTTP请求
- python发送HTTP请求
- python发送HTTP请求
- python发送HTTP请求
- python发送http请求
- python 发送http请求
- python发送HTTP请求
- python发送HTTP GET请求
- python发送HTTP GET请求
- Python发送HTTP GET请求
- python 发送http post请求
- Python之使用Python发送HTTP请求
- Python发送HTTP请求工具包httplib
- Python发送HTTP请求工具包httplib
- 使用python发送http请求代码
- python发送http请求之requests模块
- python使用urllib2发送http请求
- Python发送带header的http请求
- python编程_python通过get方式,post方式发送http请求和接收http响应_import urllib模块,import urllib2模块,import httplib模块
- Javascript如何禁止F5刷新页面和鼠标右键刷新页面?
- MSCRM年会后记
- iOS 7: 如何为iPhone 5S编译64位应用。
- Codeforces Round #219 (Div. 1)(完全)
- 鉴客 python发送HTTP请求
- html中input type="text" 的readonly 属性与asp.net服务器控件TextBox的readonly属性的js赋值、调用的小bug
- 算术表达式使用递归算法生成二叉树
- 读取xml文件转成List<T>对象的两种方法
- Levels of integration between the Portal for ArcGIS and the ArcGIS Server
- Visual Studio 2010项目中引用Lib静态库
- 读书笔记:maven生命周期和插件
- PreparedStatement与Statement区别
- Linux系统调用之open(), close()