服务器端:
#!/usr/bin/env python#-*- coding:utf-8 -*-import socketsk=socket.socket()sk.bind(('127.0.0.1',8989))sk.listen(5)Flag=Truewhile True: conn,addr=sk.accept() conn.sendall('欢迎致电10086,请输入1XXXX,0转人工服务') while Flag: client_data=conn.recv(1024) print client_data if client_data=='0': conn.sendall('转人工服务,为保证服务质量,您的通话可能被录音') elif client_data=='exit': conn.sendall('退出服务,欢迎下次再来') Flag=False else: conn.sendall('unkown!') conn.close()
客户端:
#!/usr/bin/env python#-*- coding:utf-8 -*-import socketsk=socket.socket()sk.connect(('127.0.0.1',8989))sk.settimeout(5)while True: server_data=sk.recv(1024) print server_data inp=raw_input('input:') sk.sendall(inp) if inp=='exit': breaksk.close()