Skip to content

Instantly share code, notes, and snippets.

@mmaloney
Last active May 23, 2016 08:31
Show Gist options
  • Save mmaloney/48f3a5b392d03b8e3f53 to your computer and use it in GitHub Desktop.
Save mmaloney/48f3a5b392d03b8e3f53 to your computer and use it in GitHub Desktop.
Python Web Server Application Template
{
"ID": "",
"Name": "Python Web Server",
"Description": "Python Web Server",
"Services": [
{
"Name": "Python Web Server",
"Command": "",
"Description": "Python Web Server",
"Tags": null,
"ImageID": "",
"Instances": {
"Min": 0,
"Max": 0
},
"ChangeOptions": null,
"Launch": "auto",
"HostPolicy": "",
"Hostname": "",
"Privileged": false,
"ConfigFiles": {},
"Context": null,
"Endpoints": null,
"Services": [
{
"Name": "python",
"Command": "/usr/bin/python /data/webserver.py -p 8000 -l /var/log/webserver.log",
"Description": "",
"Tags": [
"daemon"
],
"ImageID": "dockerfile/python:latest",
"Instances": {
"Min": 1,
"Max": 0
},
"ChangeOptions": null,
"Launch": "auto",
"HostPolicy": "",
"Hostname": "",
"Privileged": false,
"ConfigFiles": {
"/data/webserver.py": {
"Filename": "/data/webserver.py",
"Owner": "root",
"Permissions": "",
"Content": "import sys, getopt\nimport SimpleHTTPServer, SocketServer\nimport datetime, time\nimport logging\nfrom BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer\n\nclass SimpleHTTPRequestHandler(BaseHTTPRequestHandler): \n\u0009def log_message(self, format, *args): \n\u0009\u0009open(logfile, \"a\").write(\"%s - - [%s] %s\\n\" % \n\u0009\u0009(self.address_string(), \n\u0009\u0009self.log_date_time_string(), \n\u0009\u0009format%args)) \n\u0009def do_GET(self):\n\u0009\u0009self.send_response(200)\n\u0009\u0009self.send_header('Content-type','text/html')\n\u0009\u0009self.end_headers()\n\u0009\u0009# Send the html message\n\u0009\u0009self.wfile.write(\"\u003chtml\u003e\u003ch1\u003eHello World !\u003c/h1\u003e\u003ch2\u003eFor more information on Zenoss Control Center, check out \u003ca href='http://controlcenter.io'\u003ehttp://controlcenter.io\u003c/a\u003e\u003c/h2\u003e \u003c/html\u003e \")\n\u0009\u0009return\ndef main(argv):\n\n# Init vars \n port = 0 \n global logfile\n logfile = '' \n logconfig ='' \n# Parse CLI Args \n try:\n opts, args = getopt.getopt(argv,\"h:p:l:\",[\"port=\",\"logfile=\"])\n except getopt.GetoptError:\n print 'webserver.py -p \u003cport\u003e -l \u003clog file\u003e'\n sys.exit(2)\n for opt, arg in opts:\n if opt == '-h':\n print 'webserver.py -p \u003cport\u003e -l \u003clog file\u003e'\n sys.exit()\n elif opt in (\"-p\", \"--port\"):\n port=int(arg)\n elif opt in (\"-l\", \"--logfile\"):\n logfile = arg\n if port == 0:\n print 'invalid port specified'\n sys.exit(2)\n if logfile == '':\n print 'no log file specified'\n sys.exit(2)\n\n# Set logging config\n logging.basicConfig(level=logging.DEBUG,\n format='%(asctime)s%(name)-12s%(levelname)-8s%(message)s',\n datefmt='%m-%d %H:%M:%S',\n filename=logfile,\n filemode='w') \n fh = logging.FileHandler(logfile) \n #fh.setFormatter = ('%(asctime)s %(levelname)s %(message)s')\n logger = logging.getLogger(' WWW') \n logger.addHandler(fh)\n \n try: \n\u0009httpd = HTTPServer(('',port), SimpleHTTPRequestHandler)\n# Log webserver startup\n \u0009logger.debug('HTTP server started')\n \u0009print '%s: HTTP server listening on TCP port: %d' % (datetime.datetime.now(),port) \n# Log webserver config \n \u0009logger.info('HTTP server listening on TCP port: %s',port)\n \u0009logger.info('HTTP server logfile: %s',logfile)\n\n# Run webserver as a long running process\n httpd.serve_forever()\n except getopt.GetoptError:\n \u0009print exc.msg\n \u0009sys.exit(2)\n\nif __name__ == \"__main__\":\n main(sys.argv[1:])\n"
}
},
"Context": null,
"Endpoints": [
{
"Name": "http",
"Purpose": "export",
"Protocol": "tcp",
"PortNumber": 8000,
"PortTemplate": "",
"VirtualAddress": "",
"Application": "http",
"ApplicationTemplate": "",
"AddressConfig": {
"Port": 8000,
"Protocol": "tcp"
},
"VHosts": [
"web"
]
}
],
"Services": [],
"Tasks": null,
"LogFilters": null,
"Volumes": null,
"LogConfigs": [
{
"Path": "/var/log/webserver.log",
"Type": "apache",
"Filters": null,
"LogTags": null
}
],
"Snapshot": {
"Pause": "",
"Resume": ""
},
"RAMCommitment": 136870912,
"CPUCommitment": 0,
"Runs": null,
"Actions": {
"status": "ps -ef | grep [p]ython"
},
"HealthChecks": {
"running": {
"Script": "echo $?",
"Interval": 20
}
},
"Prereqs": null,
"MonitoringProfile": {
"MetricConfigs": null,
"GraphConfigs": null,
"ThresholdConfigs": null
},
"MemoryLimit": 0,
"CPUShares": 0,
"PIDFile": ""
}
],
"Tasks": null,
"LogFilters": null,
"Volumes": null,
"LogConfigs": null,
"Snapshot": {
"Pause": "",
"Resume": ""
},
"RAMCommitment": 1073741824,
"CPUCommitment": 2,
"Runs": null,
"Actions": null,
"HealthChecks": null,
"Prereqs": null,
"MonitoringProfile": {
"MetricConfigs": null,
"GraphConfigs": null,
"ThresholdConfigs": null
},
"MemoryLimit": 0,
"CPUShares": 0,
"PIDFile": ""
}
],
"ConfigFiles": null
}
@V3ckt0r
Copy link

V3ckt0r commented May 23, 2016

Hey @mmaloney,

Just thought I'd mention that line 32 - "ImageID": "dockerfile/python:latest", should be amended as Docker have migrated the dockerfile/ repos in github to DockerHub. See https://blog.docker.com/2015/03/updates-available-to-popular-repos-update-your-images/

The official image is now registry.hub.docker.com/_/python/

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment