Advertisement
Bkmz

Untitled

Apr 10th, 2012
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.21 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. from copy import deepcopy, copy
  3. from pdb import set_trace as st
  4.  
  5. from os import fork, pipe, read, write, close, fdopen
  6. from sys import exit
  7.  
  8. from socket import *
  9. from select import poll, POLLIN, POLLPRI, POLLHUP, POLLERR, POLLOUT
  10.  
  11. import Queue
  12.  
  13. from time import sleep
  14.  
  15. import sys
  16. reload(sys)
  17. sys.setdefaultencoding('utf8')
  18.  
  19. HOST = None
  20. PORT = 4500
  21. # Keep up with the queues of outgoing messages
  22. message_queues = {}
  23.  
  24. # poll flags
  25. READ_ONLY = POLLIN | POLLPRI | POLLHUP | POLLERR
  26. READ_WRITE = READ_ONLY | POLLOUT
  27.  
  28. for res in  getaddrinfo(HOST, PORT, AF_INET, SOCK_STREAM):
  29.     af, socktype, proto, canonname, sa = res
  30.  
  31.     # create server socket and binding it to address
  32.     server = socket(af, socktype, proto)
  33.     server.bind(sa)
  34.     server.listen(5)
  35.     server.setblocking(0)
  36.  
  37.     # Map file descriptors to socket objects
  38.     fd_to_socket = { server.fileno(): server,
  39.                    }
  40.  
  41. poller = poll()
  42. poller.register(server, READ_ONLY)
  43.  
  44. while 1:
  45.     events = poller.poll(100)
  46.  
  47.     for fd, flag in events:
  48.         s  = fd_to_socket[fd]
  49.  
  50.         # some checks
  51.         if flag & (POLLIN | POLLPRI):
  52.             if s is server:                         # new connection
  53.                 connection, client_addr = s.accept()
  54.                 print "Connected ", client_addr
  55.                 connection.setblocking(0)
  56.                 fd_to_socket[ connection.fileno() ] = connection
  57.  
  58.                 poller.register(connection, READ_ONLY)
  59.  
  60.                 message_queues[connection] = Queue.Queue()
  61.  
  62.             else:                                   # reading new data
  63.                 data = s.recv(1024)
  64.                 if data:
  65.                     print "From %s received:\n'%s'\n" % (s, data)
  66.                     poller.modify(s, READ_WRITE)
  67.                 else:                               # empty string, client disconnecting
  68.                     print "Closing connection %s after reading no data"
  69.                     poller.unregister(s)
  70.                     s.close()
  71.  
  72.                     del message_queues[s]
  73.         elif flag & POLLHUP:                        # client dirty hangup connection
  74.             print "Clossing connection from %s after HANGUP" % s.getpeername()
  75.             poller.unregister(s)
  76.             s.close()
  77.  
  78.             # del message_queues
  79.             del message_queues[s]
  80.  
  81.         elif flag & POLLOUT:                        # ready send data to client
  82.             try:
  83.                 # st()
  84.                 next_msg = message_queues[s].get_nowait()
  85.             except Queue.Empty:
  86.                 # no writable data, so stop monitoring this connection for write
  87.                 poller.modify(s, READ_ONLY)
  88.                 print "Nothing to send to %s" % str(s.getpeername())
  89.             else:
  90.                 print "Send %s to %s" % (next_msg, s.getpeername())
  91.                 s.send(next_msg)
  92.  
  93.         elif flag & POLLERR:                        # closing connection on some error
  94.             print "Clossing connection from %s after Exception" % s.getpeername()
  95.             poller.unregister(s)
  96.             s.close()
  97.  
  98.             del message_queues[s]
  99.  
  100.     # conn, addr = server.accept()
  101.     # print "Connected ", addr
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement