Python/BLIPListenerTest.py
author Jens Alfke <jens@mooseyard.com>
Mon Jul 20 13:26:29 2009 -0700 (2009-07-20)
changeset 59 46c7844cb592
parent 51 de59ce19f42e
permissions -rw-r--r--
* MYBonjourBrowser: Added delegate (no methods for it yet, just for client use.)
* MYBonjourRegistration: Added +canonicalFormOfTXTRecordDictionary:.
* MYBonjourService: Added back-reference to browser.
* IPAddress: Added conversions to/from struct sockaddr.
     1 #!/usr/bin/env python
     2 # encoding: utf-8
     3 """
     4 BLIPListenerTest.py
     5 
     6 Created by Jens Alfke on 2008-06-04.
     7 This source file is test/example code, and is in the public domain.
     8 """
     9 
    10 from BLIP import Listener
    11 
    12 import asyncore
    13 import logging
    14 import unittest
    15 
    16 
    17 class BLIPListenerTest(unittest.TestCase):
    18     
    19     def testListener(self):
    20         def handleConnection(conn):
    21             logging.info("Got new connection: %r", conn)
    22             conn.ListenerTestNumRequests = 0
    23         
    24         def handleRequest(request):
    25             logging.info("Got request!: %r",request)
    26             request.connection.ListenerTestNumRequests += 1
    27             body = request.body
    28             assert len(body)<32768
    29             assert request.contentType == 'application/octet-stream'
    30             assert int(request['Size']) == len(body)
    31             assert request['User-Agent'] != None
    32             for i in xrange(0,len(request.body)):
    33                 assert ord(body[i]) == i%256
    34             
    35             response = request.response
    36             response.body = request.body
    37             response['Content-Type'] = request.contentType
    38             response.send()
    39             if request.connection.ListenerTestNumRequests >= 50:
    40                 request.connection.close()
    41         
    42         listener = Listener(46353)
    43         listener.onConnected = handleConnection
    44         listener.onRequest = handleRequest
    45         logging.info("Listener is waiting...")
    46         
    47         try:
    48             asyncore.loop()
    49         except KeyboardInterrupt:
    50             logging.info("KeyboardInterrupt")
    51 
    52 if __name__ == '__main__':
    53     logging.basicConfig(level=logging.INFO)
    54     unittest.main()