|
@@ -0,0 +1,95 @@
|
|
1
|
+#!/usr/bin/python
|
|
2
|
+# Copyright (c) 2013, Lily Carpenter
|
|
3
|
+# All rights reserved.
|
|
4
|
+
|
|
5
|
+# Redistribution and use in source and binary forms, with or without modification,
|
|
6
|
+# are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+# * Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+# list of conditions and the following disclaimer.
|
|
10
|
+
|
|
11
|
+# * Redistributions in binary form must reproduce the above copyright notice, this
|
|
12
|
+# list of conditions and the following disclaimer in the documentation and/or
|
|
13
|
+# other materials provided with the distribution.
|
|
14
|
+
|
|
15
|
+# * Neither the name of the Lily Carpenter nor the names of its
|
|
16
|
+# contributors may be used to endorse or promote products derived from
|
|
17
|
+# this software without specific prior written permission.
|
|
18
|
+
|
|
19
|
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
20
|
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
21
|
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
+# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
|
23
|
+# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
24
|
+# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
25
|
+# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
26
|
+# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
27
|
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
28
|
+# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
29
|
+
|
|
30
|
+usage = "./ls_echo.py <port> <directory>"
|
|
31
|
+
|
|
32
|
+import sys
|
|
33
|
+import os
|
|
34
|
+import time
|
|
35
|
+import fcntl
|
|
36
|
+import socket
|
|
37
|
+import threading
|
|
38
|
+import SocketServer
|
|
39
|
+
|
|
40
|
+if len(sys.argv) != 3:
|
|
41
|
+ print usage
|
|
42
|
+ exit(1)
|
|
43
|
+
|
|
44
|
+PORT= sys.argv[1]
|
|
45
|
+DIRECTORY = sys.argv[2]
|
|
46
|
+
|
|
47
|
+port = int(PORT)
|
|
48
|
+directory = os.path.abspath(os.path.expanduser(DIRECTORY))
|
|
49
|
+if not os.path.isdir(directory):
|
|
50
|
+ raise TypeError("Path " + directory + " does not represent a valid directory.")
|
|
51
|
+
|
|
52
|
+def get_directory_listing():
|
|
53
|
+ return os.listdir("/home/lily/")
|
|
54
|
+
|
|
55
|
+class ThreadedRequestHandler(SocketServer.BaseRequestHandler):
|
|
56
|
+ def handle(self):
|
|
57
|
+ response = "\n".join(os.listdir("/home/lily/"))
|
|
58
|
+ self.request.send(response + "\n")
|
|
59
|
+
|
|
60
|
+class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
|
|
61
|
+ pass
|
|
62
|
+
|
|
63
|
+if __name__ == "__main__":
|
|
64
|
+ # No standard daemon module, thanks http://daemonize.sourceforge.net/daemonize.txt
|
|
65
|
+ process_id = os.fork()
|
|
66
|
+ if process_id < 0:
|
|
67
|
+ sys.exit(1)
|
|
68
|
+ elif process_id != 0:
|
|
69
|
+ sys.exit(0)
|
|
70
|
+
|
|
71
|
+ process_id = os.setsid()
|
|
72
|
+ if process_id == -1:
|
|
73
|
+ sys.exit(1)
|
|
74
|
+
|
|
75
|
+ devnull = '/dev/null'
|
|
76
|
+ if hasattr(os, "devnull"):
|
|
77
|
+ devnull = os.devnull
|
|
78
|
+ null_descriptor = open(devnull, 'rw')
|
|
79
|
+ for descriptor in (sys.stdin, sys.stdout, sys.stderr):
|
|
80
|
+ descriptor.close()
|
|
81
|
+ descriptor = null_descriptor
|
|
82
|
+
|
|
83
|
+ os.umask(027)
|
|
84
|
+
|
|
85
|
+ os.chdir('/')
|
|
86
|
+
|
|
87
|
+ lockfile = open('/tmp/lsecho.lock', 'w')
|
|
88
|
+ fcntl.lockf(lockfile, fcntl.LOCK_EX|fcntl.LOCK_NB)
|
|
89
|
+
|
|
90
|
+ lockfile.write('%s' %(os.getpid()))
|
|
91
|
+ lockfile.flush()
|
|
92
|
+
|
|
93
|
+ server = ThreadedTCPServer(("localhost", port), ThreadedRequestHandler)
|
|
94
|
+
|
|
95
|
+ server.serve_forever()
|