Small script that will wait for a TCP connection and respond with the contents of the configured directory.

ls_echo.py 3.0KB

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