汪晓康

导航

创建一个简单webserver客户端

# 手写一个webserver客户端
import socket
import re

sk = socket.socket()
sk.bind(('127.0.0.1', 8080))
sk.listen()

while True:
	conn, addr = sk.accept()
	data = conn.recv(9000)
	data_str = str(data, encoding='utf8')
	# print(data_str)
	res = re.match('\S+\s(\S+)\s', data_str)
	print(res)
	url = res.group(1)
	print(url)
	if url == '/yingying/':
		msg = b'hands up!'
	elif url == '/hpg/':
		msg = b'hello word'
	else:
		msg = b'404 not found!'
		conn.send(b'HTTP/1.1 200 OK\r\n\r\n')
		conn.send(msg)
		conn.close()

posted on 2021-08-09 22:49  汪晓康  阅读(65)  评论(0编辑  收藏  举报