一个简单的ip测试服务器
打印返回 请求头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')