Created
November 12, 2017 21:50
-
-
Save brejoc/80605858c2cf59fd953cf39632609b24 to your computer and use it in GitHub Desktop.
Example for SO_REUSEPORT - Start multiple instances of this script and connect to them via shared port 8080: $ echo data | nc localhost 8080
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 | |
""" | |
Example for SO_REUSEPORT | |
Usage: | |
Start multiple instances of this script and connect | |
to them via shared port 8080: $ echo data | nc localhost 8080 | |
""" | |
import os | |
import uuid | |
import socket | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) | |
s.bind(('0.0.0.0', 8080)) | |
s.listen(1) | |
print('PID: {}'.format(os.getpid())) | |
while True: | |
conn, addr = s.accept() | |
data = conn.recv(1024) | |
conn.send(str(uuid.uuid4())) | |
print("Received request from {}:{}".format(addr[0], addr[1])) | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment