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()

转载于:https://www.cnblogs.com/mylele/p/5015357.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值