Skip to content

Commit 56054ae

Browse files
author
root
committed
modified: pyhttpd
1 parent d0cc341 commit 56054ae

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

pyhttpd

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/usr/bin/env python
2+
# pyhttpd - portable python HTTP server utility
3+
24
import os
35
import sys
46
from optparse import OptionParser
@@ -12,18 +14,20 @@ else:
1214
is_root = False
1315
port = 8080
1416

15-
# Parse command line options
17+
## Parse command line options
1618
parser = OptionParser()
19+
parser.add_option('-D', '--daemon', dest='fork', action='store_true', default=False, help='Fork process to the background')
20+
# Options: bind
1721
parser.add_option('-b', '--bind', dest='host', default='0.0.0.0', help='Bind server to this ip address')
1822
parser.add_option('-p', '--port', dest='port', default=port, help='Listen for connection on this port')
23+
# Options: server modes
1924
parser.add_option('-t', '--text', dest='text', help='Text to return in "text" mode')
20-
parser.add_option('-r', '--redirect', dest='redirect', help='Location to redirect to when in "redirect" mode')
25+
parser.add_option('-r', '--redirect', dest='url', help='Location to redirect to when in "redirect" mode')
2126
parser.add_option('-f', '--file', dest='file', help='Print the contents of this file when in "file" mode')
2227
parser.add_option('-d', '--dir', dest='dir', help='Serve the contents of this directory when in "dir" mode')
23-
parser.add_option('-D', '--daemon', dest='fork', action='store_true', default=False, help='Fork process to the background')
2428
(opts, args) = parser.parse_args()
2529

26-
# Sanatize and sort input
30+
## Sanatize and sort input
2731
host = str(opts.host)
2832
port = int(opts.port)
2933
address = (host, port)
@@ -33,17 +37,17 @@ content_type = 'text/html'
3337
if opts.text:
3438
mode = 'text'
3539
text = str(opts.text)
36-
# Reditect mode
37-
elif opts.redirect:
38-
mode = 'redirect'
39-
redirect = str(opts.redirect)
4040
# Single file mode
4141
elif opts.file:
4242
mode = 'file'
43-
# @todo check file exists
43+
#@todo check file exists
4444
f = open(str(opts.file), 'r')
4545
text = f.read()
4646
f.close()
47+
# Redirect mode
48+
elif opts.url:
49+
mode = 'redirect'
50+
url = str(opts.url)
4751
# Directory listing mode
4852
elif opts.dir:
4953
mode = 'dir'
@@ -54,24 +58,24 @@ else:
5458
mode = 'text'
5559
text = 'Hi!'
5660

57-
# Server Handler class
61+
## Server handler class
5862
class MyHandler(BaseHTTPRequestHandler):
5963
# GET requests
6064
def do_GET(self):
61-
# Mode: Redirect
65+
# Mode: redirect
6266
if mode == 'redirect':
6367
self.send_response(301)
64-
self.send_header('Location', redirect)
68+
self.send_header('Location', url)
6569
self.end_headers()
6670
# Mode: text
6771
elif mode == 'text' or mode == 'file':
6872
self.send_response(200)
6973
self.send_header('Content-type',content_type)
7074
self.end_headers()
7175
self.wfile.write(text + '\n')
72-
# Mode: ditectory listing
76+
# Mode: directory listing
7377
elif mode == 'dir':
74-
# @todo ditectory listing
78+
#@todo directory listing
7579
print('something with ' + dir)
7680
# Mode: idk
7781
else:
@@ -84,12 +88,11 @@ def main():
8488
server = HTTPServer(address, MyHandler)
8589
print('Started the server on ' + host + ':' + str(port))
8690
server.serve_forever()
87-
8891
except KeyboardInterrupt:
8992
print('\n' + 'Shutting down due to Keyboard interrupt')
9093
server.shutdown()
9194

92-
# Do the UNIX double-fork magic
95+
## Do the UNIX double-fork magic
9396
def daemon(func):
9497
try: # First fork
9598
pid = os.fork()
@@ -112,11 +115,12 @@ def daemon(func):
112115
func() # Function to be forked
113116
os._exit(os.EX_OK) # Cleanly exit when all done
114117

115-
# Constructor
118+
## Constructor
116119
if __name__ == '__main__':
117120
# Run forked or regular?
118121
if not opts.fork:
119122
main()
120123
else:
121124
daemon(main)
125+
# pyhttpd
122126

0 commit comments

Comments
 (0)