标签:status lis ext 服务启动 ddr one index return 响应
打印返回 请求头request
import socket
def send_data(conn, data=None, status=200):
if status != 200 and data is None:
data = '响应失败'
try:
conn.sendall(bytes(f"HTTP/1.1 {status} OK\r\n\r\n", "utf-8")) # 响应头
conn.sendall(bytes(f"{data}", "utf-8"))
except:
return 0
return 1
def runserver(addr='127.0.0.1', port=7999, back_log=10, buffer_size=1024):
webserver = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ip_prot = (addr, port)
webserver.bind(ip_prot)
webserver.listen(back_log)
print('web服务启动: ', ip_prot)
while True:
conn, addr = webserver.accept()
recvdata = conn.recv(buffer_size)
index_text = recvdata.decode()
print('请求ip', addr)
send_data(conn, data=index_text)
conn.close()
if __name__ == '__main__':
runserver(addr='0.0.0.0')
标签:status lis ext 服务启动 ddr one index return 响应
原文地址:https://www.cnblogs.com/ham-731/p/12121189.html