# Client and Server Must be connected to same network# import socket moduleimport socket# create TCP/IP sockets = socket.socket()# get the according IP addressip_address = socket.gethostbyname(socket.gethostname())# binding ip address and ports.bind((ip_address, 12345))# listen for incoming connections (server mode) with 3 connection at a times.listen(3)# print your ip addressprint("Server ip address:", ip_address)while True:# waiting for a connection establishmentprint('waiting for a connection')connection, client_address = s.accept()try:# show connected clientprint('connected from', client_address)# sending acknowledgement to client that you are connectedconnection.send(str("Now You are connected").encode("utf-8"))# receiving the messagewhile True:data = connection.recv(1024).decode("utf-8")if data:# message from clientprint(list(client_address)[0], end="")print(": %s" % data)# Enter your message to send to clientnew_data = str(input("You: ")).encode("utf-8")connection.send(new_data)finally:# Close connectionconnection.close()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。