Python 网络编程

zhouhaihua00 2019-12-26

服务端代码:

import socket

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((‘127.0.0.1‘, 8080))
server.listen(1)
while True:
    conn, addr = server.accept()
    print(addr)
    data = conn.recv(1024)
    print(data)
    conn.send(b‘This is server.‘)
    conn.close()

客户端代码:

import socket

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((‘127.0.0.1‘, 8080))
client.send(b‘Who are you?‘)
data = client.recv(1024)
print(data)
client.close()

相关推荐