Attachment 'miniserver_fork.py'

Download

   1 #!/usr/bin/env python
   2 import socket
   3 import sys
   4 import os
   5 import signal
   6 
   7 
   8 # Skuste: 
   9 # telnet 127.0.0.1 2222
  10 # ukoncite CTRL-] a potom quit
  11 
  12 # Vytvorime TCP socket
  13 s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  14 # Naviazeme ho na port 2222 pre vsetky lokalne adresy
  15 s.bind(('',2222))
  16 # Najviac 5 spojeni bude cakat vo fronte
  17 s.listen(5)
  18 
  19 # Toto je kvoli tomu, aby nezostavali zombie 
  20 # procesy
  21 signal.signal(signal.SIGCHLD,signal.SIG_IGN)
  22 
  23 try:
  24     while True:
  25         connected_socket,address=s.accept()
  26         print 'Connected by',address
  27         pid_chld=os.fork()
  28         if pid_chld<0:
  29             print "fork vratil chybu"
  30             sys.exit(1)
  31         if pid_chld==0:
  32             while True:
  33                 data=connected_socket.recv(1024)
  34                 if not data:
  35                     break
  36             connected_socket.close()
  37             print address,'disconnected'
  38             sys.exit(0)
  39 except KeyboardInterrupt:
  40     pass
  41 s.close()

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.

You are not allowed to attach a file to this page.