Skip to content

Commit 2aa665e

Browse files
committed
add basic auth httpserver
1 parent 99b3fc2 commit 2aa665e

9 files changed

+80
-19
lines changed

.startHttpBasicAuthServer.sh.swp

12 KB
Binary file not shown.

HTTPBasicAuth_Server.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import BaseHTTPServer
2+
from SimpleHTTPServer import SimpleHTTPRequestHandler
3+
import sys
4+
import base64
5+
6+
key = ""
7+
8+
class AuthHandler(SimpleHTTPRequestHandler):
9+
def do_HEAD(self):
10+
self.send_response(200)
11+
self.send_header('Content-type', 'text/html')
12+
self.end_headers()
13+
14+
def do_AUTHHEAD(self):
15+
self.send_response(401)
16+
self.send_header('WWW-Authenticate', 'Basic realm=\"Test\"')
17+
self.send_header('Content-type', 'text/html')
18+
self.end_headers()
19+
20+
def do_GET(self):
21+
print(self.headers)
22+
global key
23+
''' Present frontpage with user authentication. '''
24+
if self.headers.getheader('Authorization') == None:
25+
self.do_AUTHHEAD()
26+
self.wfile.write('no auth header received')
27+
pass
28+
elif self.headers.getheader('Authorization') == 'Basic '+key:
29+
SimpleHTTPRequestHandler.do_GET(self)
30+
pass
31+
else:
32+
self.do_AUTHHEAD()
33+
self.wfile.write(self.headers.getheader('Authorization'))
34+
self.wfile.write('not authenticated')
35+
pass
36+
37+
def do_POST(self):
38+
body = self.rfile.read(int(self.headers['Content-Length']))
39+
print(body)
40+
print(self.headers)
41+
global key
42+
''' Present frontpage with user authentication. '''
43+
if self.headers.getheader('Authorization') == None:
44+
self.do_AUTHHEAD()
45+
self.wfile.write('no auth header received')
46+
pass
47+
elif self.headers.getheader('Authorization') == 'Basic '+key:
48+
SimpleHTTPRequestHandler.do_GET(self)
49+
pass
50+
else:
51+
self.do_AUTHHEAD()
52+
self.wfile.write(self.headers.getheader('Authorization'))
53+
self.wfile.write('not authenticated')
54+
pass
55+
def test(HandlerClass = AuthHandler,
56+
ServerClass = BaseHTTPServer.HTTPServer):
57+
BaseHTTPServer.test(HandlerClass, ServerClass)
58+
59+
60+
if __name__ == '__main__':
61+
if len(sys.argv)<3:
62+
print "usage HTTPBasicAuthServer.py [port] [username:password]"
63+
sys.exit()
64+
key = base64.b64encode(sys.argv[2])
65+
test()

HTTPS_Server.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import BaseHTTPServer,SimpleHTTPServer,SocketServer
2-
import logging
32
import cgi
43
import sys
54
import socket
@@ -8,22 +7,22 @@
87
PORT = int(sys.argv[1])
98

109
def staticHttpServer(req):
11-
logging.warning(req.headers)
10+
print(req.headers)
1211
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(req)
1312

1413
class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
1514

1615
def do_GET(self):
17-
"do something for GET"
1816
staticHttpServer(self)
1917
def do_POST(self):
20-
"do something for POST"
18+
body = self.rfile.read(int(self.headers['Content-Length']))
19+
print(body)
2120
staticHttpServer(self)
2221

2322
Handler = ServerHandler
2423

2524
httpd = BaseHTTPServer.HTTPServer(("", PORT),Handler)
2625
httpd.socket = ssl.wrap_socket (httpd.socket, certfile='../cert.pem', server_side=True)
27-
ip = socket.gethostbyname(socket.gethostname())
26+
ip = "127.0.0.1"
2827
print "Serving at: https://%(interface)s:%(port)s" % dict(interface=ip or "localhost", port=PORT)
2928
httpd.serve_forever()

HTTP_Server.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
import SimpleHTTPServer
22
import SocketServer
3-
import logging
43
import cgi
54
import sys
65
import socket
76

87
PORT = int(sys.argv[1])
98

109
def staticHttpServer(req):
11-
logging.warning(req.headers)
10+
print(req.headers)
1211
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(req)
1312

1413
class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
1514

1615
def do_GET(self):
17-
"do something for GET"
1816
staticHttpServer(self)
1917
def do_POST(self):
20-
"do something for POST"
18+
body = self.rfile.read(int(self.headers['Content-Length']))
19+
print(body)
2120
staticHttpServer(self)
2221

2322
Handler = ServerHandler
2423

2524
httpd = SocketServer.TCPServer(("", PORT), Handler)
26-
ip = socket.gethostbyname(socket.gethostname())
25+
ip = "127.0.0.1"
2726
print "Serving at: http://%(interface)s:%(port)s" % dict(interface=ip or "localhost", port=PORT)
28-
httpd.serve_forever()
27+
httpd.serve_forever()

README.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ Easy to use and make fake response over http/https
33

44
Just four steps as follows
55

6-
* Install dependent module
7-
8-
```shell
9-
npm install
10-
```
116

127
* Run build.sh script (This step is for https protocal)
138

@@ -20,7 +15,7 @@ sh buils.sh
2015
sh startHttpServer.sh
2116
```
2217

23-
* :+1: Open your browser to test (https://localhost:8001 or http://localhost:8002)
18+
* :+1: Open your browser to test (https://localhost:8001/res.json , http://localhost:8002/res.json and http://localhost:8003/res.json with Basic Authentication(default tony/1234)
2419

2520

2621

startHttpBasicAuthServer.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pushd ./resource
2+
python ../HTTPBasicAuth_Server.py 8003 tony:1234

startHttpServer.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
pushd ./resource
2-
nohup python ../HTTP_Server.py 8002 &> ../nohup.out &
2+
python ../HTTP_Server.py 8002

startHttpsServer.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
pushd ./resource
2-
nohup python ../HTTPS_Server.py 8001 &> ../nohup.out &
2+
python ../HTTPS_Server.py 8001

stopHttpBasicAuthServer.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
kill $(ps aux | grep '[H]TTPBasicAuth_Server' | awk '{print $2}')

0 commit comments

Comments
 (0)