深度优先

这个家伙好懒,除了文章什么都没留下

0%

【python】模拟Requests

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# -*- coding=utf-8 -*-
import requests

"""
模拟HttpPost请求
"""
def HttpPost(apiUrl, token, data):
try:
headers = {'Content-Type': 'application/json',
'Authorization': 'Bearer '+token}
r = requests.post(apiUrl, headers=headers, json=data).json()
return r
except:
return


"""
模拟HttpGet请求
"""
def HttpGet(apiUrl, token, data):
headers = {'Content-Type': 'application/json',
'Authorization': 'Bearer '+token}
try:
r = requests.get(apiUrl, headers=headers, data=data).json()
return r
except:
return

"""
登录获取token
"""
def GetToken():
apiUrl = 'http://88.88.88.88:8081/token'
data = "grant_type=password&username=peter&password=123123&client_id=1234&client_secret=5678"
try:
headers = {'Content-Type': 'application/json'}
result = requests.post(apiUrl, headers=headers, data=data).json()
token = result.get("access_token")
return token
except:
return

"""
开启trip
"""
def PostStartTrip(token):
apiUrl = 'http://88.88.88.88:8081/api/Trip/start'
data = {"CarId": "2", "Mass": "1111"}
try:
result = HttpPost(apiUrl, token, data)
tripId = result.get("tripId")
return tripId
except:
return

"""
上传Trip数据
"""
def UploadTripInfo(token, tripId):
apiUrl = "http://88.88.88.88:8081/api/Trip/upload"
TripData = [
{"DTime": 121212, "LAT": 11111, "LON": 11212, "Speed": 1121,
"Altitude": 1212, "AccT": 1212, "AccX": 1212, "AccY": 121, "AccZ": 1212},
{"DTime": 121212, "LAT": 11111, "LON": 11212, "Speed": 1121,
"Altitude": 1212, "AccT": 1212, "AccX": 1212, "AccY": 121, "AccZ": 1212}
]
data = {"TripId": tripId, "TripData": TripData}
HttpPost(apiUrl, token, data)

# # 登录
# token = GetToken()
# # 开启trip
# tripId = PostStartTrip(token)
# # 上传Trip数据
# UploadTripInfo(token,tripId)


# res = requests.get("http://bfsdfs.com")
# print(type(res))
# print(res.status_code)
# res.encoding = "utf-8"
# # print(res.text)
# print(res.cookies)

response = requests.get("http://www.baidu.com")
#打印请求页面的状态(状态码)
print(type(response.status_code),response.status_code)
#打印请求网址的headers所有信息
print(type(response.headers),response.headers)
#打印请求网址的cookies信息
print(type(response.cookies),response.cookies)
#打印请求网址的地址
print(type(response.url),response.url)
#打印请求的历史记录(以列表的形式显示)
print(type(response.history),response.history)