Skip to content

Instantly share code, notes, and snippets.

@YieldNull
Last active April 15, 2020 11:09
Show Gist options
  • Save YieldNull/fe8d4df7e1c5a093d83fd7036c16ebb8 to your computer and use it in GitHub Desktop.
Save YieldNull/fe8d4df7e1c5a093d83fd7036c16ebb8 to your computer and use it in GitHub Desktop.
Fork processes to handle TCP requests.
import os
import socket
def main():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('127.0.0.1', 9999))
sock.listen()
os.fork()
while True:
sock_req, addr = sock.accept()
payload = b''
while True:
data = sock_req.recv(4096)
if not data:
break
payload += data
print(os.getpid(), str(payload, 'utf-8'))
sock_req.sendall(
b'HTTP/1.1 200 OK\r\n'
+ b'Date: Sun, 18 Oct 2012 10:36:20 GMT\r\n'
+ b'Server: Apache/2.2.14 (Win32)\r\n'
+ b'Content-Length: 0\r\n'
+ b'Connection: Closed\r\n'
+ b'Content-Type: text/html;\r\n'
)
sock_req.shutdown(socket.SHUT_WR)
sock_req.close()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment