首页 > 代码库 > Socket

Socket

#server.py
import socket

ip_port = (127.0.0.1,9999)
sk = socket.socket()
sk.bind(ip_port)
sk.listen(5)

while True:
    print(server waiting connecting....)
    conn,addr = sk.accept()
    client_data = conn.recv(1024)
    print(str(client_data,utf8))
    conn.sendall(bytes(不要回答,不要回答,不要回答!,utf8))
sk.close()
#---------------------------------------------------------- 
#
client.py

import socket
ip_port
= (127.0.0.1,9999)
sk = socket.socket()
sk.connect(ip_port)
sk.sendall(bytes(
请求占领地球,utf8))
server_data
= sk.recv(1024)

print(str(server_data,utf8))
sk.close()

 

#server.py
import socket

ip_port = (127.0.0.1, 9999)

sk = socket.socket()
sk.bind(ip_port)
sk.listen(5)

while True:
    print(server waiting....)
    conn, addr = sk.accept()
    while True:
        client_data = conn.recv(1024)
        print(str(client_data,utf8))
        user = input(>>>:)
        conn.sendall(bytes(user,utf8))

sk.close()


#client.py
import socket

ip_port = (127.0.0.1, 9999)

sk = socket.socket()
sk.connect(ip_port)

while True:
    user = input(>>>:).strip()
    sk.sendall(bytes(user,utf8))
    server_reply = sk.recv(1024)
    print(str(server_reply,utf8))

sk.close()

 

Socket