1 Star 0 Fork 0

Role/Python

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
server.py 1.26 KB
一键复制 编辑 原始数据 按行查看 历史
Shrenikjangada 提交于 2020-10-01 14:59 +08:00 . Update server.py
# 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()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/CRole/Python.git
git@gitee.com:CRole/Python.git
CRole
Python
Python
master

搜索帮助