1
1
#!/usr/bin/env python
2
+ # pyhttpd - portable python HTTP server utility
3
+
2
4
import os
3
5
import sys
4
6
from optparse import OptionParser
@@ -12,18 +14,20 @@ else:
12
14
is_root = False
13
15
port = 8080
14
16
15
- # Parse command line options
17
+ ## Parse command line options
16
18
parser = OptionParser ()
19
+ parser .add_option ('-D' , '--daemon' , dest = 'fork' , action = 'store_true' , default = False , help = 'Fork process to the background' )
20
+ # Options: bind
17
21
parser .add_option ('-b' , '--bind' , dest = 'host' , default = '0.0.0.0' , help = 'Bind server to this ip address' )
18
22
parser .add_option ('-p' , '--port' , dest = 'port' , default = port , help = 'Listen for connection on this port' )
23
+ # Options: server modes
19
24
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' )
21
26
parser .add_option ('-f' , '--file' , dest = 'file' , help = 'Print the contents of this file when in "file" mode' )
22
27
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' )
24
28
(opts , args ) = parser .parse_args ()
25
29
26
- # Sanatize and sort input
30
+ ## Sanatize and sort input
27
31
host = str (opts .host )
28
32
port = int (opts .port )
29
33
address = (host , port )
@@ -33,17 +37,17 @@ content_type = 'text/html'
33
37
if opts .text :
34
38
mode = 'text'
35
39
text = str (opts .text )
36
- # Reditect mode
37
- elif opts .redirect :
38
- mode = 'redirect'
39
- redirect = str (opts .redirect )
40
40
# Single file mode
41
41
elif opts .file :
42
42
mode = 'file'
43
- # @todo check file exists
43
+ #@todo check file exists
44
44
f = open (str (opts .file ), 'r' )
45
45
text = f .read ()
46
46
f .close ()
47
+ # Redirect mode
48
+ elif opts .url :
49
+ mode = 'redirect'
50
+ url = str (opts .url )
47
51
# Directory listing mode
48
52
elif opts .dir :
49
53
mode = 'dir'
@@ -54,24 +58,24 @@ else:
54
58
mode = 'text'
55
59
text = 'Hi!'
56
60
57
- # Server Handler class
61
+ ## Server handler class
58
62
class MyHandler (BaseHTTPRequestHandler ):
59
63
# GET requests
60
64
def do_GET (self ):
61
- # Mode: Redirect
65
+ # Mode: redirect
62
66
if mode == 'redirect' :
63
67
self .send_response (301 )
64
- self .send_header ('Location' , redirect )
68
+ self .send_header ('Location' , url )
65
69
self .end_headers ()
66
70
# Mode: text
67
71
elif mode == 'text' or mode == 'file' :
68
72
self .send_response (200 )
69
73
self .send_header ('Content-type' ,content_type )
70
74
self .end_headers ()
71
75
self .wfile .write (text + '\n ' )
72
- # Mode: ditectory listing
76
+ # Mode: directory listing
73
77
elif mode == 'dir' :
74
- # @todo ditectory listing
78
+ #@todo directory listing
75
79
print ('something with ' + dir )
76
80
# Mode: idk
77
81
else :
@@ -84,12 +88,11 @@ def main():
84
88
server = HTTPServer (address , MyHandler )
85
89
print ('Started the server on ' + host + ':' + str (port ))
86
90
server .serve_forever ()
87
-
88
91
except KeyboardInterrupt :
89
92
print ('\n ' + 'Shutting down due to Keyboard interrupt' )
90
93
server .shutdown ()
91
94
92
- # Do the UNIX double-fork magic
95
+ ## Do the UNIX double-fork magic
93
96
def daemon (func ):
94
97
try : # First fork
95
98
pid = os .fork ()
@@ -112,11 +115,12 @@ def daemon(func):
112
115
func () # Function to be forked
113
116
os ._exit (os .EX_OK ) # Cleanly exit when all done
114
117
115
- # Constructor
118
+ ## Constructor
116
119
if __name__ == '__main__' :
117
120
# Run forked or regular?
118
121
if not opts .fork :
119
122
main ()
120
123
else :
121
124
daemon (main )
125
+ # pyhttpd
122
126
0 commit comments