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