Skip to content

Instantly share code, notes, and snippets.

@ramunasd
Created June 6, 2016 19:58
Show Gist options
  • Save ramunasd/90229d7db167374ec5b6b34be20f98a4 to your computer and use it in GitHub Desktop.
Save ramunasd/90229d7db167374ec5b6b34be20f98a4 to your computer and use it in GitHub Desktop.
High performance python3 single color service, can work as WMS service mock.
#!/usr/bin/env python
import os
import http.server
import socketserver
from urllib.parse import urlsplit, parse_qs
from PIL import Image
class wmsHandler(http.server.BaseHTTPRequestHandler):
# Handler for the GET requests
def do_GET(self):
url = urlsplit(self.path)
query = parse_qs(url.query)
if 'height' not in query or 'width' not in query:
self.send_response(404, 'Wrong params')
self.end_headers();
return
image = Image.new('RGB', (int(query.get('width')[0]), int(query.get('height')[0])), (255, 255, 255))
format = query.get('format', ['jpeg'])[0]
format = format.lower().split('/', 2).pop()
self.send_response(200)
self.send_header('Content-type', 'image/' + format)
self.end_headers()
# Send the html message
image.save(self.wfile, format)
return
try:
# Create a web server and define the handler to manage the
# incoming request
port = int(os.environ.get('PORT', 8080))
server = socketserver.ThreadingTCPServer(('', port), wmsHandler)
print('Started WMS color server on port', port)
# Wait forever for incoming htto requests
server.serve_forever()
except KeyboardInterrupt:
print('shutting down server')
server.shutdown()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment