Python/BLIPListenerTest.py
author morrowa
Tue Jun 23 12:46:40 2009 -0700 (2009-06-23)
changeset 53 e9f209a24d53
parent 13 84c2d38f924c
child 56 6c3b5372a307
permissions -rw-r--r--
Connections opened by listeners now close correctly.
     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 handleRequest(request):
    21             logging.info("Got request!: %r",request)
    22             body = request.body
    23             assert len(body)<32768
    24             assert request.contentType == 'application/octet-stream'
    25             assert int(request['Size']) == len(body)
    26             assert request['User-Agent'] != None
    27             for i in xrange(0,len(request.body)):
    28                 assert ord(body[i]) == i%256
    29             
    30             response = request.response
    31             response.body = request.body
    32             response['Content-Type'] = request.contentType
    33             response.send()
    34         
    35         listener = Listener(46353)
    36         listener.onRequest = handleRequest
    37         logging.info("Listener is waiting...")
    38         
    39         try:
    40             asyncore.loop()
    41         except KeyboardInterrupt:
    42             logging.info("KeyboardInterrupt")
    43 
    44 if __name__ == '__main__':
    45     logging.basicConfig(level=logging.DEBUG)
    46     unittest.main()