Last active
August 29, 2015 14:06
-
-
Save cocoajin/fb788840a33fcbe84fdb to your computer and use it in GitHub Desktop.
httplib http post,get, 请求
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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/x-www-form-urlencoded","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" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment