Skip to content

Instantly share code, notes, and snippets.

@malexer
Created January 28, 2014 10:02
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save malexer/8664997 to your computer and use it in GitHub Desktop.
Save malexer/8664997 to your computer and use it in GitHub Desktop.
Hello World HTTP server in pyzmq (using ZeroMQ RAW socket) - a Python version of http://gist.github.com/hintjens/5480625
import zmq
DEFAULT_PAGE = '\r\n'.join([
"HTTP/1.0 200 OK",
"Content-Type: text/plain",
"",
"Hello, World!",
])
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
# send Hello World page
router.send_multipart([identity, DEFAULT_PAGE])
# Close connection to browser
router.send_multipart([identity, ''])
@vaibhavsoni
Copy link

there seems to be some error in line 24 of this code while using it in python 3.3.3
error - "Type error - can't use unicode, use send_string" instead.
i tried doing this but doesn't work.

@mtasic85
Copy link

mtasic85 commented Dec 8, 2019

This should fix the issue:

DEFAULT_PAGE = '\r\n'.join([
    "HTTP/1.0 200 OK",
    "Content-Type: text/plain",
    "",
    "Hello, World!",
]).encode()

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