Skip to content

Instantly share code, notes, and snippets.

@hiway
Forked from malexer/zmq_http_server_example.py
Last active July 11, 2020 06:53
Show Gist options
  • Save hiway/df70ecd912aa4b33409e68bd92c4efa3 to your computer and use it in GitHub Desktop.
Save hiway/df70ecd912aa4b33409e68bd92c4efa3 to your computer and use it in GitHub Desktop.
Hello World HTTP server in pyzmq (using ZeroMQ RAW socket) - a Python3.5+ version of http://gist.github.com/hintjens/5480625
# coding=utf-8
import zmq
DEFAULT_PAGE = '\r\n'.join([
"HTTP/1.0 200 OK",
"Content-Type: text/plain",
"",
"Hello, World!",
])
DEFAULT_PAGE = DEFAULT_PAGE.encode('utf-8') # Ensure we use bytes, not str.
context = zmq.Context()
router = context.socket(zmq.ROUTER)
router.router_raw = True
router.bind('tcp://*:8080')
while True:
msg = router.recv_multipart()
identity, request = msg
response = DEFAULT_PAGE
# send Hello World page
router.send_multipart([identity, response])
# Close connection to browser
router.send_multipart([identity, b''])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment