python2 httplib 筆記

weixin_34041003發表於2014-09-25

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"

 

相關文章