-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·62 lines (54 loc) · 2.38 KB
/
server.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
'''
LANBox Copyright (c) 2013 William Quade
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
''' This is the Python web server app for the desktop. It has only been tested on Mac OS X. '''
import cgi, os, SocketServer, sys, time, urllib
from SimpleHTTPServer import SimpleHTTPRequestHandler
from StringIO import StringIO
class DirectoryHandler(SimpleHTTPRequestHandler):
if not os.path.exists(os.path.expanduser("~/LANBox")):
os.makedirs(os.path.expanduser("~/LANBox"))
os.chdir(os.path.expanduser("~/LANBox"))
def list_directory(self, path):
try:
list = os.listdir(path)
except os.error:
self.send_error(404, "Error")
return None
list.sort(key=lambda a: a.lower())
f = StringIO()
displaypath = cgi.escape(urllib.unquote(self.path))
f.write("<ul>\n")
for name in list:
fullname = os.path.join(path, name)
displayname = linkname = name
date_modified = time.ctime(os.path.getmtime(fullname))
if os.path.isdir(fullname):
displayname = name + "/"
linkname = name + "/"
if os.path.islink(fullname):
displayname = name + "@"
f.write('<li><a href="%s "> %s</a>\n'
% (urllib.quote(linkname), cgi.escape(displayname)))
f.write("</ul>")
length = f.tell()
f.seek(0)
self.send_response(200)
encoding = sys.getfilesystemencoding()
self.send_header("Content-type", "text/html; charset=%s" % encoding)
self.send_header("Content-Length", str(length))
self.end_headers()
return f
httpd = SocketServer.TCPServer(("", 58077), DirectoryHandler)
print "serving at port", 58077
httpd.serve_forever()