Created
July 19, 2022 21:03
-
-
Save alpacas9/8eaedc938c3381cbd4ba7ff0ce1f55e5 to your computer and use it in GitHub Desktop.
python socket rst
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 socket | |
| import struct | |
| import time | |
| def tcp_handler(client_sock, addr): | |
| try: | |
| print('new connection from %s:%s' % addr) | |
| time.sleep(1) | |
| finally: | |
| # close connection directly | |
| client_sock.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', 1, 0)) | |
| print('close connection from %s:%s' % addr) | |
| client_sock.close() | |
| if __name__ == '__main__': | |
| sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
| PORT = 3000 | |
| sock.bind(('', PORT)) | |
| sock.listen(5) | |
| print('starting listening socket on port: %s' % PORT) | |
| while 1: | |
| client_sock, addr = sock.accept() | |
| tcp_handler(client_sock, addr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment