简单TCP Server的python实现
client#!/usr/bin/python3import socket#Create socket objectclientsocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)#host = '192.168.1.104'host = socket.gethostname()port = 444clientsocket.connec
·
client
#!/usr/bin/python3
import socket
#Create socket object
clientsocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
#host = '192.168.1.104'
host = socket.gethostname()
port = 444
clientsocket.connect(('192.168.1.104', port)) #You can substitue the host with the server IP
#Receiving a maximum of 1024 bytes
message = clientsocket.recv(1024)
clientsocket.close()
print(message.decode('ascii'))
server
#!/usr/bin/python3
import socket
#Creating the socket object
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname() #Host is the server IP
port = 444 #Port to listen on
#Binding to socket
serversocket.bind((host, port)) #Host will be replaced/substitued with IP, if changed and not running on host
#Starting TCP listener
serversocket.listen(3)
while True:
#Starting the connection
clientsocket,address = serversocket.accept()
print("received connection from " % str(address))
#Message sent to client after successful connection
message = 'hello! Thank you for connecting to the server' + "\r\n"
clientsocket.send(message)
clientsocket.close()

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。
更多推荐
所有评论(0)