用Python写的一个多线程机器人聊天程序
注意:python版本必须为3.x +, 不兼容2.x版本的python
文件列表:
main_server.py: 服务端程序,用于接收客户端的信息, 并返回Jinko回答的话
1 # tcp server 2 import socket; 3 import time; 4 import threading; 5 from JinkoRobot import *; 6 7 #应用程序入口类 8 class ApplicationServer: 9 10 #构造函数初始化 socket 11 def __init__(self, host="localhost", port=8005): 12 self.connList = []; 13 self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM); 14 self.socket.bind((host, port)); 15 self.socket.listen(100); 16 print("我是Jinko, 我来自[图灵机器人:http://www.tuling123.com]"); 17 print(""); 18 print("赶紧打开客户端和我聊天吧!"); 19 self.accept(); 20 21 #多线程接受用户请求 22 def accept(self): 23 while True: 24 connection, address = self.socket.accept(); 25 # print('connect') 26 thread = ChatThread(connection); 27 thread.start(); 28 29 #聊天线程 30 class ChatThread(threading.Thread): 31 32 def __init__(self, conn): 33 threading.Thread.__init__(self); 34 self.__connection = conn; 35 36 def run(self): 37 while True: 38 try: 39 recv = self.__connection.recv(8192); 40 except: 41 break; 42 43 # print("收到:" + recv.decode('utf-8')) 44 rebot = JinkoRobot(); 45 rebot.listenFor(recv.decode('utf-8')); 46 answer = rebot.answer(); 47 # print('say:' + answer) 48 self.__connection.send(answer.encode('utf-8')); 49 50 ApplicationServer();
main_client.py: 客户端程序, 用于和Jinko发起聊天
import socket; import time; sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM); print("正在和Jinko连线..."); sock.connect(('localhost', 8005)); print(""); while True: speak = input("和Jinko说点什么:"); if speak == "quit": break; if speak == "": continue; # print("发送中..." + "("+ speak +")") sock.send(speak.encode('utf-8')); # print("已发送") print("Jinko在思考..."); answer = sock.recv(8192); print("Jinko回复你:" + answer.decode('utf-8')); print(""); sock.close();
JinkoRobot.py: 它就是Jinko啦啦啦~~
#Jinko Robot import json; import urllib.request; import urllib.parse; class JinkoRobot: __answer = ''; def __init__(self): pass; #倾听话语 def listenFor(self, string): self.__answer = self.thinking(string); # 思考着 def thinking(self, string): says = urllib.parse.quote_plus(string); f = urllib.request.urlopen("http://www.tuling123.com/openapi/api?key=4bc32d41c10be18627438ae45eb839ac&info=" + says); json_str = f.read(); thinkdata = json.loads(json_str.decode('utf-8')); f.close(); if(thinkdata['code'] > 40000 and thinkdata['code'] < 40010): return "今天Jinko被你问得有点累了, 过会再问吧!"; if(thinkdata['code'] == 200000): return thinkdata['text'] + ", 猛戳这里>>" + thinkdata['url']; if(thinkdata['code'] == 302000) : info = thinkdata['text']; for content in thinkdata['list']: info += "\n\n>" + content['article'] \ + " 来源于" + content['source'] \ + " 详细信息请猛戳这里>>" + content['detailurl']; return info; if(thinkdata['code'] == 305000): info = thinkdata['text']; for key in thinkdata['list']: info += "\n\n>" + key + ": 车次>" + content['trainnum'] \ + " 从" + content['start'] + "到" + content['terminal'] \ + " 发车时间:" + content['starttime'] \ + " 到达时间:" + content['endtime'] \ + " 详细信息请猛戳这里>>" + content['detailurl']; return info; return thinkdata['text']; #和你交流回答 def answer(self): return self.__answer;
运行效果图:
原文连接: https://www.cnblogs.com/JinkoWu/p/5051837.html