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.
jens@13
     1
#!/usr/bin/env python
jens@13
     2
# encoding: utf-8
jens@13
     3
"""
jens@13
     4
BLIPListenerTest.py
jens@13
     5
jens@13
     6
Created by Jens Alfke on 2008-06-04.
jens@13
     7
This source file is test/example code, and is in the public domain.
jens@13
     8
"""
jens@13
     9
jens@13
    10
from BLIP import Listener
jens@13
    11
jens@13
    12
import asyncore
jens@13
    13
import logging
jens@13
    14
import unittest
jens@13
    15
jens@13
    16
jens@13
    17
class BLIPListenerTest(unittest.TestCase):
jens@13
    18
    
jens@13
    19
    def testListener(self):
morrowa@56
    20
        def handleConnection(conn):
morrowa@56
    21
            logging.info("Got new connection: %r", conn)
morrowa@56
    22
            conn.ListenerTestNumRequests = 0
morrowa@56
    23
        
jens@13
    24
        def handleRequest(request):
jens@13
    25
            logging.info("Got request!: %r",request)
morrowa@56
    26
            request.connection.ListenerTestNumRequests += 1
jens@13
    27
            body = request.body
jens@13
    28
            assert len(body)<32768
jens@13
    29
            assert request.contentType == 'application/octet-stream'
jens@13
    30
            assert int(request['Size']) == len(body)
jens@13
    31
            assert request['User-Agent'] != None
jens@13
    32
            for i in xrange(0,len(request.body)):
jens@13
    33
                assert ord(body[i]) == i%256
jens@13
    34
            
jens@13
    35
            response = request.response
jens@13
    36
            response.body = request.body
jens@13
    37
            response['Content-Type'] = request.contentType
jens@13
    38
            response.send()
morrowa@56
    39
            if request.connection.ListenerTestNumRequests >= 50:
morrowa@56
    40
                request.connection.close()
jens@13
    41
        
jens@13
    42
        listener = Listener(46353)
morrowa@56
    43
        listener.onConnected = handleConnection
jens@13
    44
        listener.onRequest = handleRequest
jens@13
    45
        logging.info("Listener is waiting...")
jens@13
    46
        
jens@13
    47
        try:
jens@13
    48
            asyncore.loop()
jens@13
    49
        except KeyboardInterrupt:
jens@13
    50
            logging.info("KeyboardInterrupt")
jens@13
    51
jens@13
    52
if __name__ == '__main__':
morrowa@56
    53
    logging.basicConfig(level=logging.INFO)
jens@13
    54
    unittest.main()