标签:ever ftp read exit sockets thread base request while
socketserver —— 实现ftp server端和client端的交互
import socketserver class MyServer(socketserver.BaseRequestHandler): def handle(self): conn = self.request conn.sendall(bytes(‘欢迎致电 10086,请输入1xxx,0转人工服务.‘,encoding=‘utf-8‘)) Flag = True while Flag: data = conn.recv(1024).decode(‘utf-8‘) if data == ‘exit‘: Flag = False elif data == ‘0‘: conn.sendall(bytes(‘通过可能会被录音.balabala一大推‘,encoding=‘utf-8‘)) else: conn.sendall(bytes(‘请重新输入.‘,encoding=‘utf-8‘)) if __name__ == ‘__main__‘: server = socketserver.ThreadingTCPServer((‘127.0.0.1‘,8008),MyServer) server.serve_forever()
import socket ip_port = (‘127.0.0.1‘,8008) sk = socket.socket() sk.connect(ip_port) sk.settimeout(5) while True: data = sk.recv(1024).decode(‘utf-8‘) print(‘receive:‘,data) inp = input(‘please input:‘) sk.sendall(bytes(inp,encoding=‘utf-8‘)) if inp == ‘exit‘: break sk.close()
标签:ever ftp read exit sockets thread base request while
原文地址:http://www.cnblogs.com/big-handsome-guy/p/7644304.html