-
Notifications
You must be signed in to change notification settings - Fork 47
/
cs_client.py
47 lines (38 loc) · 1.37 KB
/
cs_client.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Simple chat client to communicate with chat script server
# Not very efficient, since it uses a thread per socket model,
# If servicing a large number of clients, twisted may be a better fit
import socket
class Chatbot:
def __init__(self):
pass
def response(self, message):
user = "anhv"
botname = "hoaian"
msg = '%s\u0000%s\u0000%s\u0000' % (user, botname, message)
msg = msg.encode()
output = self.sendAndReceiveChatScript(msg)
return output
def sendAndReceiveChatScript(self, msgToSend, server='127.0.0.1', port=1024, timeout=10):
try:
# Connect, send, receive and close socket. Connections are not persistent
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(timeout) # timeout in secs
s.connect((server, port))
s.sendall(msgToSend)
msg = ''
while True:
chunk = s.recv(1024)
if chunk == b'':
break
msg = msg + chunk.decode("utf-8", errors="ignore")
s.close()
return msg
except Exception as e:
print(e)
return None
def build_chatbot_engine():
bot = Chatbot()
bot.response(":build HoaiAn")
bot.response(":reset")
if __name__ == '__main__':
print("Chat Script client")