Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
1,371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.22 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3.  
  4.  
  5. import socket
  6.  
  7. import os
  8.  
  9. import signal
  10.  
  11. import time
  12.  
  13. import random
  14.  
  15. # Open a socket.
  16.  
  17. server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  18.  
  19. server_socket.bind(("", 8080))
  20.  
  21. server_socket.listen(128)
  22.  
  23.  
  24.  
  25. # For keeping track of child process pids.
  26.  
  27. N = 1
  28.  
  29. wpids = []
  30.  
  31.  
  32.  
  33. for _ in range(N):
  34.  
  35.     wpid = os.fork()
  36.  
  37.     wpids.append(wpid)
  38.  
  39.     if wpid == 0:
  40.  
  41.         while True:
  42.  
  43.             connection, address = server_socket.accept()
  44.  
  45.             print (connection.recv (1024).decode())
  46.  
  47.             time.sleep(random.randint(1,3))
  48.  
  49.             connection.send("HTTP/1.1 200 OK\n".encode());  ????
  50.  
  51.             connection.send("Content-length: 46\n".encode()); ?????
  52.  
  53.             connection.send("Content-Type: text/html\n\n".encode()); ?????
  54.  
  55.             connection.send("<html><body><H1>Hello world</H1></body></html>".encode()); ??????????
  56.  
  57.             connection.close()
  58.  
  59.  
  60.  
  61. # Forward any relevant signals to the child processes.
  62.  
  63. def handler(signum, frame):
  64.  
  65.     for wpid in wpids:
  66.  
  67.         os.kill(wpid, signum)
  68.  
  69.  
  70.  
  71. signal.signal(signal.SIGINT, handler)
  72.  
  73. signal.signal(signal.SIGQUIT, handler)
  74.  
  75.    
  76.  
  77. for _ in range(N):
  78.  
  79.     os.wait()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement