Skip to content

Instantly share code, notes, and snippets.

@andreipak
Forked from tsuna/server.py
Last active August 29, 2015 14:16
Show Gist options
  • Save andreipak/a34cb82157556c1b93d3 to your computer and use it in GitHub Desktop.
Save andreipak/a34cb82157556c1b93d3 to your computer and use it in GitHub Desktop.

Revisions

  1. @tsuna tsuna created this gist Jan 5, 2012.
    69 changes: 69 additions & 0 deletions server.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    #!/usr/bin/python
    # A simple example of a threaded TCP server in Python.
    #
    # Copyright (c) 2012 Benoit Sigoure All rights reserved.
    #
    # Redistribution and use in source and binary forms, with or without
    # modification, are permitted provided that the following conditions are met:
    # - Redistributions of source code must retain the above copyright notice,
    # this list of conditions and the following disclaimer.
    # - Redistributions in binary form must reproduce the above copyright notice,
    # this list of conditions and the following disclaimer in the documentation
    # and/or other materials provided with the distribution.
    # - Neither the name of the StumbleUpon nor the names of its contributors
    # may be used to endorse or promote products derived from this software
    # without specific prior written permission.
    # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    # POSSIBILITY OF SUCH DAMAGE.

    import SocketServer
    import os
    import subprocess
    import sys

    def daemonize():
    if os.fork() != 0:
    os._exit(0)

    os.setsid()

    if os.fork() != 0:
    os._exit(0)

    os.chdir("/")
    os.umask(022)
    [os.close(i) for i in xrange(3)]
    os.open(os.devnull, os.O_RDWR)
    os.dup2(0, 1)
    os.dup2(0, 2)

    class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
    allow_reuse_address = True

    class Handler(SocketServer.StreamRequestHandler):
    def handle(self):
    while True:
    command = self.rfile.readline()
    if not command: # EOF
    break
    self.wfile.write("your command was: %s" % (command,))

    def main(args):
    daemonize()
    server = ThreadedTCPServer(("0.0.0.0", 4242), Handler)
    try:
    server.serve_forever()
    except KeyboardInterrupt:
    pass

    if __name__ == "__main__":
    sys.exit(main(sys.argv))