python2 httplib 笔记
#coding=utf-8 ‘‘‘ Created on 2014年9月25日 @author: cocoajin ‘‘‘ import httplib,urllib base=‘httpbin.org‘ #不需要添加 "http://" con=httplib.HTTPConnection(base) ip = ‘/ip‘ con.request(‘GET‘,ip) re=con.getresponse() print re.getheaders() print re.read() con.close() #GET con=httplib.HTTPConnection(base) parm={‘name‘:‘nick‘,‘age‘:18} gets=‘/get‘ con.request(‘GET‘, gets+‘?‘+urllib.urlencode(parm)) re=con.getresponse() print re.getheaders() print re.read() con.close() #POST con=httplib.HTTPConnection(base) parm={‘name‘:‘nick‘,‘age‘:18} posts=‘/post‘ #headers = {"Content-type":"application/json","Accept":"text/plain"} json headers = {"Content-type":"application/json","Accept":"text/plain"} #form con.request(‘POST‘, posts,urllib.urlencode(parm),headers) re=con.getresponse() print re.getheaders() print re.read() con.close() #文件上传使用 "multipart/form-data"
时间: 2024-10-08 01:56:50