Last active
December 13, 2015 21:49
-
-
Save pklaus/4980542 to your computer and use it in GitHub Desktop.
Debian Linux – Deploying a Pure Python Web App
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3.3 | |
HOST_NAME = '::' | |
PORT_NUMBER = 80 | |
import time | |
from whatsmyip import HTTPServerV6, RequestHandler | |
# http://stackoverflow.com/a/2699996/183995 | |
import os, pwd, grp | |
def drop_privileges(uid_name='nobody', gid_name='nogroup'): | |
if os.getuid() != 0: | |
# We're not root so, like, whatever dude | |
return | |
# Get the uid/gid from the name | |
running_uid = pwd.getpwnam(uid_name).pw_uid | |
running_gid = grp.getgrnam(gid_name).gr_gid | |
# Remove group privileges | |
os.setgroups([]) | |
# Try setting the new uid/gid | |
os.setgid(running_gid) | |
os.setuid(running_uid) | |
# Ensure a very conservative umask | |
old_umask = os.umask(77) | |
httpd = HTTPServerV6((HOST_NAME, PORT_NUMBER), RequestHandler) | |
print(time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)) | |
drop_privileges('whatsmyip', 'webapp') | |
try: | |
httpd.serve_forever() | |
except KeyboardInterrupt: | |
pass | |
httpd.server_close() | |
print(time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
### BEGIN INIT INFO | |
# Provides: WhatsMyIP | |
# Required-Start: $remote_fs $network | |
# Required-Stop: $remote_fs $network | |
# Should-Start: $all | |
# Should-Stop: $all | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: WhatsMyIP Service in Python | |
# Description: WhatsMyIP is a service that returns your | |
# IP address when you query the HTTP server. | |
# https://github.com/pklaus/WhatsMyIP/ | |
### END INIT INFO | |
import sys, time | |
from daemon import Daemon | |
HOST_NAME = '::' | |
PORT_NUMBER = 8080 | |
# http://stackoverflow.com/a/2699996/183995 | |
import os, pwd, grp | |
def drop_privileges(uid_name='nobody', gid_name='nogroup'): | |
if os.getuid() != 0: | |
return | |
running_uid = pwd.getpwnam(uid_name).pw_uid | |
running_gid = grp.getgrnam(gid_name).gr_gid | |
os.setgroups([]) | |
os.setgid(running_gid) | |
os.setuid(running_uid) | |
old_umask = os.umask(77) | |
class MyDaemon(Daemon): | |
def run(self): | |
from whatsmyip import HTTPServerV6, RequestHandler | |
httpd = HTTPServerV6((HOST_NAME, PORT_NUMBER), RequestHandler) | |
print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER) | |
drop_privileges('whatsmyip', 'webapp') | |
try: | |
httpd.serve_forever() | |
except KeyboardInterrupt: | |
pass | |
httpd.server_close() | |
print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER) | |
if __name__ == "__main__": | |
daemon = MyDaemon('/tmp/whatsmyip-daemon.pid') | |
if len(sys.argv) == 2: | |
if 'start' == sys.argv[1]: | |
daemon.start() | |
elif 'stop' == sys.argv[1]: | |
daemon.stop() | |
elif 'restart' == sys.argv[1]: | |
daemon.restart() | |
else: | |
print "Unknown command" | |
sys.exit(2) | |
sys.exit(0) | |
else: | |
print "usage: %s start|stop|restart" % sys.argv[0] | |
sys.exit(2) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment