pythonhttplib
1. python3么有httplib了吗
1、有,python3把httplib改了名字,对应的库是http.client
2、网址是:
https://docs.python.org/3.4/library/http.client.html
https://docs.python.org/2/library/httplib.html
2. 请教个python httplib2传递参数问题
楼主的理解没有问题啊
.
python中函数的实参传递规则是:
标注了参数名的就要按参数名传递,打乱顺序的情况下一定要加参数名,否则会混乱的。
没有缺省的实参情况下就会依次传递,如果不够的话,后面的会自动去取自己的缺省值。
如果实参的数量比
3. 怎么用python使用pip安装httplib模块
直接使用命令安装即可,以下为安装命令:
切需到pip所在目录,执行
pip install httplib
4. python httplib怎么打印发送的请求
python有一个httplib的库,提供了很方便的方法实现GET和POST请求,只需要简单的组织一下即可。
python发送get请求代码:
#!/usr/bin/env python
#coding=utf8
import httplib
httpClient = None
try:
httpClient = httplib.HTTPConnection('localhost', 80, timeout=30)
httpClient.request('GET', '/test.php')
#response是HTTPResponse对象
response = httpClient.getresponse()
print response.status
print response.reason
print response.read()
except Exception, e:
print e
finally:
if httpClient:
httpClient.close()
发送POST请求
#!/usr/bin/env python
#coding=utf8
import httplib, urllib
httpClient = None
try:
params = urllib.urlencode({'name': 'tom', 'age': 22})
headers = {"Content-type": "application/x-www-form-urlencoded"
, "Accept": "text/plain"}
httpClient = httplib.HTTPConnection("localhost", 80, timeout=30)
httpClient.request("POST", "/test.php", params, headers)
response = httpClient.getresponse()
print response.status
print response.reason
print response.read()
print response.getheaders() #获取头信息
except Exception, e:
print e
finally:
if httpClient:
httpClient.close()
5. python的httplib,urllib和urllib2的区别及用
urllib和urllib2
urllib 和urllib2都是接受URL请求的相关模块,但是urllib2可以接受一个Request类的实例来设置URL请求的headers,urllib仅可以接受URL。
这意味着,你不可以伪装你的User Agent字符串等。
urllib提供urlencode方法用来GET查询字符串的产生,而urllib2没有。这是为何urllib常和urllib2一起使用的原因。
目前的大部分http请求都是通过urllib2来访问的
httplib
httplib实现了HTTP和HTTPS的客户端协议,一般不直接使用,在python更高层的封装模块中(urllib,urllib2)使用了它的http实现。
urllib简单用法
urllib.urlopen(url[, data[, proxies]]) :
[python] view plain
google = urllib.urlopen('http://www.google.com')
print 'http header:/n', google.info()
print 'http status:', google.getcode()
print 'url:', google.geturl()
for line in google: # 就像在操作本地文件
print line,
google.close()
详细使用方法见
urllib学习
urllib2简单用法
最简单的形式
[python] view plain
import urllib2
response=urllib2.urlopen('http://www.douban.com')
html=response.read()
实际步骤:
1、urllib2.Request()的功能是构造一个请求信息,返回的req就是一个构造好的请求
2、urllib2.urlopen()的功能是发送刚刚构造好的请求req,并返回一个文件类的对象response,包括了所有的返回信息。
3、通过response.read()可以读取到response里面的html,通过response.info()可以读到一些额外的信息。