-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
executable file
·73 lines (68 loc) · 2.29 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
#!/usr/bin/env python
###
# Simple python API server for honeypot stats
#
# Copyright (c) 2012 by ManiacTwister
#
###
# GNU General Public Licence (GPL)
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA 02111-1307 USA
###
from BaseHTTPServer import BaseHTTPRequestHandler
from StringIO import StringIO
class HTTPRequest(BaseHTTPRequestHandler):
def __init__(self, request_text):
self.rfile = StringIO(request_text)
self.raw_requestline = self.rfile.readline()
self.error_code = self.error_message = None
self.parse_request()
def send_error(self, code, message):
self.error_code = code
self.error_message = message
import socket
allowedhost = '176.9.44.144'
#allowedhost = '81.217.14.212'
host = ''
port = 13342
backlog = 5
size = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
s.bind((host,port))
s.listen(backlog)
while 1:
client, address = s.accept()
cfile = client.makefile('rw',0)
data = client.recv(size)
if data and address[0] == allowedhost:
request = HTTPRequest(data)
if request.path == '/stats':
f = open("/root/stats.json", "r")
try:
string = f.read()
finally:
f.close()
cfile.write('HTTP/1.0 200 OK\r\n')
cfile.write("Content-Type: text/plain\r\n\r\n")
cfile.write(string)
elif request.path == '/':
cfile.write('HTTP/1.0 200 OK\r\n')
cfile.write("Content-Type: text/html\r\n\r\n")
cfile.write('<html><body><h1>/</body></html>')
else:
cfile.write('HTTP/1.0 200 OK\r\n')
cfile.write("Content-Type: text/html\r\n\r\n")
cfile.write('<html><body><h1>'+request.path+'</body></html>')
cfile.close()
client.close()