HTTP请求

Posted minghong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HTTP请求相关的知识,希望对你有一定的参考价值。

#__author__="G"
#date: 2019/4/11
import requests

from week_9.common.config import config


class HttpCookies:
def http_request(self,url,data,method,cookies=None,json=None):
url=config.get(‘api‘,‘pre_url‘)+url #url拼接
if method.lower()==‘get‘:
resp=requests.get(url=url,params=data,cookies=cookies)
elif method.lower()==‘post‘:
if json:
resp=requests.post(url=url,json=data,cookies=cookies)
else:
resp=requests.post(url=url,data=data,cookies=cookies)
# print (‘‘‘响应报文:{}
# 响应头:{}
# 状态码:{}
# 响应cookie:{}
# 请求cookies:{}‘‘‘
# .format(resp.text
# ,resp.headers,resp.status_code,resp.cookies,resp.request._cookies))
return (resp.text)


class HttpSessions:
def __init__(self):
# 打开一个session
self.session = requests.sessions.session()

def http_request(self, method, url, data=None, json=None):
url = config.get(‘api‘, ‘pre_url‘) + url
if method.lower() == ‘get‘:
resp = self.session.request(method=method, url=url, params=data)
elif method.lower() == ‘post‘:
if json:
resp = self.session.request(method=method, url=url, json=data)
else:
resp = self.session.request(method=method, url=url, data=data)

else:
print(‘UN-support method‘)
return (resp.text)
# print(‘‘‘响应报文:{}
# 响应头:{
# 状态码:{}
# 响应cookie:{}
# 请求cookies:{}‘‘‘
# .format(resp.text
# , resp.headers, resp.status_code, resp.cookies, resp.request._cookies))


def close(self):
self.session.close()
if __name__==‘__main__‘:
#注册
# url = ‘http://test.lemonban.com/futureloan/mvc/api/member/register‘
# main = {‘mobilephone‘: ‘17625188012‘, ‘pwd‘: ‘1234561234561234561‘}
# G=HttpCookies()
# G.http_request(url,main,‘GET‘)
# print(G)
# # 登录
url = ‘http://test.lemonban.com/futureloan/mvc/api/member/login‘
main = {‘mobilephone‘: ‘‘, ‘pwd‘: ‘‘}
# G=HttpCookies()
b=HttpCookies().http_request(url,main,‘post‘)
print(b)
#
# #充值
# main = {"mobilephone": "18861342700", "amount": "1000"}
# url=‘http://test.lemonban.com/futureloan/mvc/api/member/recharge‘
# G=HttpCookies()
# c=G.http_request(url,main,‘post‘,b)
# print(c)
# #提现
# main = {"mobilephone": "18861342700", "amount": "1000"}
# url=‘http://test.lemonban.com/futureloan/mvc/api/member/withdraw‘
# G=HttpCookies()
# d=G.http_request(url,main,‘post‘,b)
# print(d)

以上是关于HTTP请求的主要内容,如果未能解决你的问题,请参考以下文章

HTTP协议HTTP请求和响应

HTTP请求方法

HTTP协议:HTTP请求和相关API

HTTP请求行、请求头、请求体详解

Spring HTTP请求过程

HTTP请求报文结构