python 对于http Request 请求常见处理方式

What is HTTP?
HTTP is a set of protocols designed to enable communication between clients and servers. It works as a request-response protocol between a client and server.
A web browser may be the client, and an application on a computer that hosts a web site may be the server.
So, to request a response from the server, there are mainly two methods:
GET : to request data from the server.
POST : to submit data to be processed to the server.
【翻译】HTTP是一系列协议设计来保证客户端和服务器的通信。它在CS间通过请求回应协议工作。一个网络浏览器可能是一个客户端,电脑上的一个应用可能是服务端。所以,为了从服务端请求回应,通常有两种方式:get: 从服务器端请求数据。 POST:提交数据到服务器处理。
 

# coding:utf-8
__author__ = 'wayne'

import requests
import json

headers={'content-type': 'application/json'}
data = {'type': 'loginTypes_REQUEST', 'uname': 'i', 'upwd': '804dc20036dbd8313ed055'}
url="http://wr.com/v1/api/account/login"
url1="http://wr.com/v1/api/ArticleTask/GetTaskListByUser?"

def apiTest(method, url, data ,headers):
        try:
            if method == "post":
                results = requests.post(url=url, data=json.dumps(data), headers=headers)
            if method == "get":
                results = requests.get(url=url, params=data, headers=headers)
            if method == "put":
                results = requests.put(url, data=json.dumps(data), headers=headers)
            if method == "delete":
                results = requests.delete(url, headers=headers)
            if method == "options":
                results == requests.options(url, headers=headers)
            #response = results.json()
            return results
        except Exception as e:
            logging.error("service is error", e)

		
r= apiTest("post",url,data,headers)
mm = r.json()['data']['accesstoken']
print(r.status_code)
print(mm)

prma={'accesstoken':mm}
rr = apiTest('get',url1,prma,headers)
#print(rr.status_code==requests.codes.ok)
print(rr.json().get('code'))

we can use several HTTP libraries like:

猜你喜欢

转载自blog.csdn.net/wangweimic/article/details/89151635