Python 官方文档:入门教程 => 点击学习
server.py:import Socketdef main(): host = '127.0.0.1' port = 5000 server_socket = socket.socket() server_soc
server.py:
import Socket
def main():
host = '127.0.0.1'
port = 5000
server_socket = socket.socket()
server_socket.bind((host, port))
server_socket.listen(1)
print('***** Server Started *****')
conn, addr = server_socket.accept()
print('Connection from: ' + str(addr))
while True:
data = conn.recv(1024).decode()
print('from connected user: ' + str(data))
if not data:
data = 'NO DATA'
else:
data = str(data).upper()
print('sending: ' + str(data))
conn.send(data.encode())
conn.close()
client.py:
import socket
def main():
host = '127.0.0.1'
port = 5000
client_socket = socket.socket()
client_socket.connect((host, port))
message = input(' -> ')
while message != 'q':
client_socket.send(message.encode())
data = client_socket.recv(1024).decode()
print('Receive from server: ' + data)
message = input(' -> ')
client_socket.close()
if __name__ == '__main__':
main()
--结束END--
本文标题: python 3 socket
本文链接: https://lsjlt.com/news/192067.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0