python server, client

华凌
2023-12-01


# python3.3 server

import socket, sys

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 5100))
s.listen(5)
client, addr = s.accept()

while 1:
    buf = client.recv(1024)
    if not buf: break
    client.sendall(buf)

client.close()
s.close()

# python3.3 client

import socket, sys

host = '127.0.0.1'
port = 5100

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))

while 1:
    mysend = input('please enter a string:')
    s.sendall(mysend.encode())
    myrecv = s.recv(1024)
    if not myrecv: break
    print(bytes(myrecv).decode())

s.close()


 类似资料: