python模拟服务器,处理浏览器请求

import socket


def deal(txt):
    txt = str(txt)
    all = txt.split(r'\r\n')
    num = len(all)
    dict = {}
    for i in range(num):
        try:
            if i == 0:
                key = all[i].split('/', 1)[0]
                value = all[i].split('/', 1)[1]
                dict[key] = value
            key = all[i].split(':', 1)[0]
            value = all[i].split(':', 1)[1]
            dict[key] = value
        except:
            continue
    return dict

s = socket.socket()

host = '192.168.1.123'   # 自己本机 IP 
port = 9999

s.bind((host,port))
s.listen(10)

print('开始监听')
while 1:
	conn,addr = s.accept()
	data = conn.recv(1024)
	a = deal(data)
	print(a)

以上就是服务器,
python模拟服务器,处理浏览器请求_第1张图片

你可能感兴趣的:(Python)