-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwww.py
More file actions
127 lines (101 loc) · 2.92 KB
/
www.py
File metadata and controls
127 lines (101 loc) · 2.92 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/python2
import sys
import os
import socket
import telnetlib
import threading
from struct import pack, unpack
def recvuntil(sock, txt):
d = ""
while d.find(txt) == -1:
try:
dnow = sock.recv(1)
if len(dnow) == 0:
print "-=(warning)=- recvuntil() failed at recv"
print "Last received data:"
print d
return False
except socket.error as msg:
print "-=(warning)=- recvuntil() failed:", msg
print "Last received data:"
print d
return False
d += dnow
return d
def recvall(sock, n):
d = ""
while len(d) != n:
try:
dnow = sock.recv(n - len(d))
if len(dnow) == 0:
print "-=(warning)=- recvuntil() failed at recv"
print "Last received data:"
print d
return False
except socket.error as msg:
print "-=(warning)=- recvuntil() failed:", msg
print "Last received data:"
print d
return False
d += dnow
return d
# Proxy object for sockets.
class gsocket(object):
def __init__(self, *p):
self._sock = socket.socket(*p)
def __getattr__(self, name):
return getattr(self._sock, name)
def recvall(self, n):
return recvall(self._sock, n)
def recvuntil(self, txt):
return recvuntil(self._sock, txt)
class Handler(threading.Thread):
def __init__(self, s, addr):
super(Handler, self).__init__()
self.s = s
self.addr = addr
def return_http(self, data, status=200, status_text="OK",
mime="application/binary"):
self.s.sendall("HTTP/1.1 %i %s\r\n" % (status, status_text))
self.s.sendall("Content-Type: %s\r\n" % mime)
self.s.sendall("Content-Length: %i\r\n" % len(data))
self.s.sendall("\r\n")
self.s.sendall(data)
def run(self):
data = recvuntil(self.s, "\r\n\r\n").splitlines()
verb, path, ver = data[0].split(" ")
try:
if ".." in path or not path.startswith("/"):
raise Exception("bye")
if path == "/":
path = "/index.html"
final_path = "public_html" + path
with open(final_path, "rb") as f:
d = f.read()
_, ext = os.path.splitext(path)
mime = {
".html": "text/html;charset=utf-8",
".png": "image/png",
}
mtype = "application/binary"
if ext in mime:
mtype = mime[ext]
self.return_http(d, mime=mtype)
except:
self.return_http("NO!", status=403, status_text="Forbidden",
mime="text/plain;charset=utf-8")
self.s.shutdown(socket.SHUT_RDWR)
self.s.close()
def main():
server = gsocket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(("0.0.0.0", 8080))
server.listen(5)
while True:
conn, addr = server.accept()
print "[ INFO ] New connection: %s:%i" % addr
th = Handler(conn, addr)
th.daemon = False
th.start()
if __name__ == "__main__":
main()