1、http请求:
GET:用来请求已被URI识别的资源,指定的资源经服务器端解析后返回响应内容,最常用于向服务器查询某些信息,所有的名-值对都必须由&分离
POST:用来传输实体的主体,
get 方法用户信息获取,它是安全的,而post方式是用于修改服务器上资源的请求
get请求的数据会附在url之后,而post方法提交的数据则放置在http报文实体的主体里,所以post方法的安全性比get方法要高
get方法传输的数据量一般限制在2KB,POST方法对于数据大小无限制
PUT:用来传输文件,
DELETE:删除文件,指明客户端想让服务删除某个资源
HEAD:获得报文首部,无实体
OPTIONS:用来查询针对请求URI指定资源支持的方法
TRACE:追踪路径
CONNECT:要求在与代理服务器通信时建立隧道,实现用隧道协议进行TCP通信。主要使用ssl和tls协议把通信内容加密后经网络隧道传输。
# -*- coding=utf-8 -*- import requests import json #get请求 r=requests.get("https://github.com/timeline.json") #post请求 r1=requests.post("http://m.ctrip.com/post") #获取响应内容 #print r.content #以字节的方式去显示,中文显示为字符 #print r.text #以文本的方式去显示 #print r1.content #print r1.text #url传递参数 payload={'keyword': '日本', 'salecityid': '2'} r2=requests.get("http://m.ctrip.com/webapp/tourvisa/visa_list", params=payload) #print r2.url #获取/修改网页编码 r3=requests.get('https://github.com/timeline.json') #print r.encoding r3.encoding="utf-8" #json处理 r4=requests.get('https://github.com/timeline.json') #print r4.json() #定制请求头 url="http://m.ctrip.com" headers={'User-Agent' : 'Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 4 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19'} r5=requests.post(url,headers=headers) #print r5.request.headers #复杂post请求 url1="http://m.ctrip.com" payload1={'some': 'data'} r6=requests.post(url,data=json.dumps(payload1)) #print r6 #响应状态码 r7=requests.get('http://m.ctrip.com') #print r7.status_code #响应头 r8=requests.get('http://m.ctrip.com') #print r8.headers #print r8.headers['Content-Type'] #print r8.headers.get('content-type') #读取cookie url2='http://example.com/some/cookie/setting/url' r9=requests.get(url2) #print r9.cookies['example_cookie_name'] #设置超时时间 r10=requests.get('http://m.ctrip.com',timeout=0.001)