# tcp_ser.py
import socket
def main():
# create socket
ser_sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# bind
ser_sock.bind(("192.168.40.129", 8080))
# listen
ser_sock.listen(1000)
while True:
# accept
print("waiting for client... ...")
cli_sock,cli_addr = ser_sock.accept()
# print user info
print("a guest arrived(%s)" % str(cli_addr))
while True:
# recv msg
recv_data = cli_sock.recv(1024)
print("recvived msg:%s" % recv_data.decode("utf-8"))
if recv_data:
result = eval(recv_data.decode("utf-8"))
cli_sock.send(str(result).encode("utf-8"))
else:
break
# after communication,close cli_sock
cli_sock.close()
print("sign off.")
ser_sock.close()
if __name__ == "__main__":
main()
Tcp-Server
最新推荐文章于 2024-09-04 21:22:47 发布