Skip to content

Instantly share code, notes, and snippets.

@cocoajin
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cocoajin/fb788840a33fcbe84fdb to your computer and use it in GitHub Desktop.
Save cocoajin/fb788840a33fcbe84fdb to your computer and use it in GitHub Desktop.
httplib http post,get, 请求
#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