-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathhttpsvrgar.py
executable file
·66 lines (53 loc) · 1.43 KB
/
httpsvrgar.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
#!/usr/bin/python
# v 0.1
# Copyleft Thanat0s
# http://Thanat0s.trollprod.org
#
# Licence GNU GPL
# A file 2 "basic" Socket Webserver...
import socket
import re
import sys
if __name__ == '__main__':
HOST = None # Symbolic name meaning all available interfaces
PORT = 50007 # Arbitrary non-privileged port
if len(sys.argv) < 2:
print 'Serve a file.. This file should have headers and body'
print 'Examples:'
print ' - serve itself ' + sys.argv[0] + './' + sys.argv[0]
sys.exit()
fromfile = sys.argv[1]
print ( 'I will serve %s on port %d' ) % (fromfile,PORT )
with open(fromfile, 'rb') as f:
filearray = f.readlines()
s = None
for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE):
af, socktype, proto, canonname, sa = res
try:
s = socket.socket(af, socktype, proto)
except socket.error as msg:
s = None
continue
try:
s.bind(sa)
s.listen(1)
except socket.error as msg:
s.close()
s = None
continue
break
if s is None:
print 'could not open socket'
sys.exit(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
data = conn.recv(1024)
if not data:
break
else:
if re.match('^GET ', data):
print ('Received %s' ) % (data)
for lines in filearray:
conn.send(lines)
conn.close()