1.UDP
#UDP
#server
import socket
udp_sk = socket.socket(type = socket.SOCK_DGRAM)
udp_sk.bind(('127.0.0.1', 9000))
msg, addr = udp_sk.recvfrom(1024)
print(msg)
udp_sk.sendto(b'hi', addr)
udp_sk.close()
#client
import socket
ip_port=('127.0.0.1', 9000)
udp_sk = socket.socket(type=socket.SOCK_DGRAM)
udp_sk.sendto(b'hello', ip_port)
back_msg, addr = udp_sk.recvfrom(1024)
print(back_msg.decode('utf-8'), addr)
2.浏览器
import socket
server = socket.socket()
server.bind(('127.0.0.1', 9001))
server.listen()
conn, addr = server.accept()
from_client_msg = conn.recv(1024)
print(from_client_msg.decode('utf-8'))
conn.send(b'HTTP/1.1 200 ok\r\n\r\n')
with open('one.html','rb') as f:
data = f.read()
# conn.send(b'<h1>hello world</h1>')
conn.send(data)
conn.close()
server.close()