cshenfu
2023-03-28 60edbee179c2a5af004f85799544346c9bdabfd8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import os
import socket
 
remote_IP='127.0.0.1'
remote_port=5555
 
remote_addr=(remote_IP,remote_port)
socket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
 
#绑定IP地址和端口号
socket.bind(remote_addr)
#监听来自客户端的信息
socket.listen()
print('正在监听来自客户端的信息......')
 
#new_socket用于与客户机的对话
new_socket,addr=socket.accept()
while True:
    print('结束对话: (Q or q)')
    str=new_socket.recv(1024).decode('utf-8')
    if str=='Q' or str=='q':
        print('服务器结束对话')
        new_socket.close()
        break
    else:
        print(str)