-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.py
105 lines (89 loc) · 2.43 KB
/
server.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import datetime
import os
import pickle
import socket
import sys
import threading
import time
from subprocess import run
from _thread import *
try:
from howdoi.howdoi import howdoi
except ImportError:
run(["pip", "install", "howdoi"])
from howdoi.howdoi import howdoi
host = ""
port = 8337
CONNECTION_LIST = []
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
def get_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# doesn"t even have to be reachable
s.connect(("10.255.255.255", 1))
IP = s.getsockname()[0]
except:
IP = "127.0.0.1"
finally:
s.close()
return IP
def howdoitommy(query):
n = 1
while True:
if n > 10:
return "sorry, I cannot answer that"
args = {
"query": query, #.split(" "),
"num_answers": 1,
"pos": n,
"all": False,
"color": False,
"explain": False,
"clear_cache": False,
"json_output": False,
"version": False,
"search_engine": "google",
"save": False,
"view": False,
"remove": False,
"empty": False,
"sanity_check": False,
"link": False
}
answer = howdoi(args)
if len(answer) < 50 or len(answer) > 600:
n += 1
continue
else:
return answer
def threaded_client(conn):
while True:
data = conn.recv(2048)
if not data:
break
dataArray = data.decode().split(";")
if dataArray[0] == "howdoi":
print(str(dataArray[1]))
answer = howdoitommy(dataArray[1])
conn.sendall(str.encode(answer))
elif dataArray[0] == "print_to_terminal":
print(str(dataArray[1]))
conn.close()
print("Socket created")
try:
s.bind((host, port))
except socket.error as e:
print(str(e))
sys.exit()
print("Socket bounded")
s.listen(10)
CONNECTION_LIST.append(s)
print("Socket is ready. Waiting for requests ")
print("ADD THIS IP TO AHK FILE...")
IP = get_ip()
print(str(IP))
while True:
conn, addr = s.accept()
print("connected to: " + addr[0] + ":" + str(addr[1]))
start_new_thread(threaded_client, (conn,))