Server
from multiprocessing import Process
import socket
def func(conn):
while True:
res = conn.recv(1024)
if len(res) == 0:
break
res = res.decode('utf-8')
usr, pwd = res.split(':')
if usr == 'admin' and pwd == 'admin':
msg = '登录成功!'
else:
msg = '您不是管理员!'
print(msg)
conn.send(msg.encode("utf-8"))
conn.close()
if __name__ == '__main__':
s = socket.socket()
s.bind(('127.0.0.1', 8080))
s.listen(5)
while True:
conn, addr = s.accept()
t = Process(target=func, args=(conn,))
t.start()
Client
import socket
s = socket.socket()
s.connect(('127.0.0.1', 8080))
while True:
msg = input("请输入用户名和密码,用:隔开")
s.send(msg.encode('utf-8'))
data = s.recv(1024)
print(data.decode('utf-8'))