python 模拟get,post,delete,put请求应该怎么写


方法一:

使用httplib

import httplib

hostname="localhost"

port=80

method="get" #get,post,put,delete

url="/api/userinfo/get"

body_param='{"uid":3}'

httpheader_json={"content-Type":"application/json","Accept":"application/json"}

 

httpClient=httplib.HTTPConnection(hostname,port)

httpClient.request(method,url,body_param,httpheader_json)

response=httpClient.getresponse()

status=response.status

reason=response.reason

response_header=response.getheaders()

response_body=response.read()

 

方法二:

使用urllib2

import urllib2

url="http://localhost"

param='{"userid":1}'

request=urllib2.Request(url.param)

request.add_header("Content-Type","application/json")

request.add_header("Accept","application/json")

request.get_method=lambda :"GET"#"GET,POST,PUT,DELETE"

response=urllib2.urlopen(request)

response_txt=response.read()

response_header=response.info()


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM