Last active
February 1, 2020 10:20
-
-
Save g10guang/fd831e653fd9cbb1e558e4f914cc13e2 to your computer and use it in GitHub Desktop.
python code create socket reuseaddr with subprocess and reuseaddr + reuseport
This file contains hidden or 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
import os | |
import socket | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) | |
print('create socket') | |
s.bind(('0.0.0.0', 19823)) | |
print('bind') | |
s.listen(5) | |
print('listen') | |
pid = os.fork() | |
while True: | |
conn_socket, addr = s.accept() | |
print('conn_socket={} addr={} pid={}'.format(conn_socket, addr, os.getpid())) | |
conn_socket.send(b'msg from pid={}'.format(os.getpid())) | |
conn_socket.close() |
This file contains hidden or 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
nc 127.0.0.1 19823 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment