在Python中实现HTTP长连接,通常指的是使用HTTP持久连接(也称为HTTP keep-alive)。HTTP持久连接允许在一次TCP连接中发送和接收多个HTTP请求/响应,而不是为每个请求/响应打开和关闭连接。这减少了建立和关闭连接的开销,提高了传输效率。
import json
import random
import requests
data = None
url = "http://127.0.0.1:12201/gelf"
def requests_post():
session = requests.session()
with open("log.log", encoding="utf-8") as f:
lines = f.readlines()
for line in lines:
print(line.encode('utf-8'))
r = session.post(url, data=line.encode('utf-8'), verify=False)
print(r)
if __name__ == '__main__':
requests_post()