Python Socket

服务器端

import socket

s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind(('127.0.0.1',8889))
s.listen(5)
while True:
    conn,addr=s.accept();
    print(addr,"connected!")
    while True:
        data=conn.recv(1024)
        if not data:
            break
        print("Client send:",data)
        conn.send(b"Server recived!")
    conn.close()

客户端

import socket

s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(('127.0.0.1',8889))
while True:
    str=input()
    s.sendall(str.encode())
    str=s.recv(1024)
    print(str)

猜你喜欢

转载自blog.csdn.net/mryangll/article/details/58337314