server 端: #!/usr/bin/env python # -*- coding:utf-8 -*- import SocketServer class MyServer(SocketServer.BaseRequestHandler): def handle(self): # print self.request,self.client_address,self.server conn = self.request conn.sendall('欢迎致电 10086,请输入1xxx,0转人工服务.') Flag = True while Flag: data = conn.recv(1024) if data == 'exit': Flag = False elif data == '0': conn.sendall('你的通话可能会被录音.balabala一大推') else: conn.sendall('请重新输入.') if __name__ == '__main__': server = SocketServer.ForkingTCPServer(('127.0.0.1',8009),MyServer) server.serve_forever() client 端: #!/usr/bin/env python # -*- coding:utf-8 -*-
import socket
client = socket.socket()
ip_port = ('127.0.0.1',8009)
client.connect(ip_port)
while True:
data = client.recv(1024)
print data
inp = raw_input("客户端输入:")
client.send(inp)
if inp == 'exit':
break