-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcph_server.py
executable file
·94 lines (79 loc) · 2.28 KB
/
cph_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
#!/bin/env python
# cph_server.py
# 2014-06-30
from sys import exit
from argparse import ArgumentParser,Action # argparse documentation: https://docs.python.org/dev/library/argparse.html
# class executableClass(Action):
# def __call__(self, parser, namespace, values, option_string=None):
# return None
parser = ArgumentParser(
prog=__file__,
description='',
usage='%(prog)s [options] search_term')
# parser.add_argument(
# '--do-nothing',
# nargs=0,
# action=executableClass,
# help='')
#
# parser.add_argument(
# '-c', '--categories',
# nargs='+',
# metavar='',
# choices=['ABC','DEF'],
# help='')
#
# parser.add_argument(
# '-p','--pages',
# default=0,
# type=int,
# help='')
#
# parser.add_argument(
# '--count',
# action='store_true',
# help='')
#
# parser.add_argument(
# 'useless_things',
# nargs='+',
# help='')
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from subprocess import Popen,PIPE
import urlparse
class MyHandler(BaseHTTPRequestHandler):
def do_HEAD(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
def do_GET(self):
self.do_HEAD()
def BAD_INPUT(self, post_data):
self.send_response(700)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(post_data)
def do_POST(self):
length = int(self.headers['Content-Length'])
post_data = urlparse.parse_qs(self.rfile.read(length).decode('utf-8'))
try:
print(post_data)
out, err = Popen(['cph','-w',post_data['word'][0],'-s',post_data['salt'][0]], stdout=PIPE, stderr=PIPE).communicate()
self.wfile.write(out)
except KeyError:
self.BAD_INPUT(post_data)
def main(namespace):
try:
HOST_NAME = 'CPH'
PORT = 12123
server = HTTPServer(('', PORT), MyHandler)
print("serving at port " + str(PORT))
server.serve_forever()
except KeyboardInterrupt:
print('^C received, shutting down server')
server.socket.close()
if __name__ == '__main__':
args = parser.parse_args()
main(args)
exit(0)
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4