python中网络相关的代码片
Easul Lv4

HTTP请求

折叠代码块PYTHON 复制代码
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
# 可用postman自动生成
import json
import requests

def httpRequest():
url = "https://www.so.com"
headers = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36 UOS"
}
getParams = {
"q": "112"
}
postParams = {
"q": "112"
}
jsonData = {
"first": 1
}
# params用于get请求的参数
# data用于post请求的参数,也可以是文件
# json用于请求体
response = requests.request("GET", url, headers = headers, params=getParams, data=postParams, json=jsonData, timeout= 5)
# 获取响应码
print(response.status_code)
# 获取响应头
headers = response.headers
for key in headers:
print(f"{key}: {headers[key]}")
# 获取响应体
responseText = response.text
# 响应体为json时可以这样解析
print(json.loads(response.text))
# 也可以直接响应为json
print(response.json().get("key"))
# 将响应体一行一行保存
with open("./test1.txt", "w", encoding="UTF-8") as file:
for line in responseText:
file.write(line)
# 将信息保存下来,用于保存二进制数据
with open("./test2.txt", "wb") as file:
file.write(response.content)

httpRequest()

HTTP请求视频流

HTTP请求中,请求视频流时如果卡住,可以在requests.request添加stream=True

请求时手动携带cookie

折叠代码块PYTHON 复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import os
import pickle
import requests

def httpRequestWithCookie():
url = "https://www.so.com"
if (os.path.exists("./cookies")):
with open(f"./cookies", "rb",) as file:
cookies = pickle.load(file)
response = requests.get(url, cookies = cookies)
else:
response = requests.get(url)
cookies = response.cookies
with open(f"./cookies", "wb") as file:
pickle.dump(cookies, file)

httpRequestWithCookie()

请求时自动携带cookie

折叠代码块PYTHON 复制代码
1
2
3
4
5
6
7
8
9
import requests

def httpRequestWithSession():
url = "https://www.so.com"
session = requests.session()
response = session.request("GET", url);
print(response.text)

httpRequestWithSession()

socks5代理

折叠代码块BASH 复制代码
1
2
# 先安装依赖
pip3 install requests[socks]
折叠代码块PYTHON 复制代码
1
2
3
4
5
6
7
8
import requests

proxies = {'http' : 'socks5://127.0.00.1:1080',
'https': 'socks5://127.0.00.1:1080'}

url= 'http://test.com'
r = requests.get(url, proxies=proxies)
print(r.text)
 评论
来发评论吧~
Powered By Valine
v1.5.2