Skip to content

Commit

Permalink
WSGIServer moved to separate process.
Browse files Browse the repository at this point in the history
  • Loading branch information
drmats committed Jan 11, 2014
1 parent af501d5 commit 2419586
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# pyfup

Basic file upload WSGI application (python 2.x/3.x)
Basic file upload WSGI application (python >=2.6.x/>=3.x)

This script brings up a
[simple\_server](http://docs.python.org/3.3/library/wsgiref.html#module-wsgiref.simple_server)
Expand Down Expand Up @@ -72,4 +72,5 @@ for more details.
## notes

The script was tested and is known to work with python versions 2.7.2, 2.7.3,
2.7.5, 3.3.0, 3.3.2 and 3.3.3 on linux and windows.
2.7.5, 3.3.0, 3.3.2 and 3.3.3 on linux and windows, but should also work on
python version >=2.6.x.
41 changes: 28 additions & 13 deletions fup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Basic file upload WSGI application (python 2.x/3.x).
"""Basic file upload WSGI application (python >=2.6.x/>=3.x).
This script brings up a simple_server from python's wsgiref
package and runs a really simple web application on it.
Expand Down Expand Up @@ -177,7 +177,8 @@ def html (head=common_head, body=""):



# ...
# FieldStorage class subclassed for override the default choice
# of storing all files in a temporary directory.
class FUPFieldStorage(FieldStorage):

"""multipart/form-data request body parser"""
Expand Down Expand Up @@ -343,16 +344,15 @@ def __init__ (self):
"""Program entry point."""
args = self.parse_args()
signal.signal(
signal.SIGINT,
Main.exit_handler
signal.SIGINT, self.exit_handler
)
print(
"[%s] -- Hi there! -- [%s:%u]"
% (software_version, args.host, args.port)
print("[%s] -- exit: ctrl+C" % software_version)
self.server_process = Process(
target=self.run_server,
args=(args.host, args.port)
)
make_server(
args.host, args.port, Application()
).serve_forever()
self.server_process.start()
self.main_loop()


def parse_args (self):
Expand All @@ -376,17 +376,32 @@ def parse_args (self):
return argparser.parse_args()


@staticmethod
def exit_handler (sig_num, stack_frame):
def exit_handler (self, sig_num, stack_frame):
"""SIGINT/KeyboardInterrupt handler."""
self.server_process.terminate()
print("\nBye!")
sys.exit()


def run_server (self, host, port):
"""WSGIServer main loop."""
print("listening on %s:%u" % (host, port))
make_server(
host, port, Application()
).serve_forever()


def main_loop (self):
"""Main process loop (just to keep it alive)."""
while True:
input()




# ...
if __name__ == "__main__":
from multiprocessing import Process
Main()
else:
elif __name__ != "__parents_main__":
app = Application()

0 comments on commit 2419586

Please sign in to comment.