debuggers.hg

view tools/python/xen/web/tcp.py @ 4655:a838a908e38e

bitkeeper revision 1.1327.2.2 (4267a9b3MhPpljnjQ5IbfLdzcW2K3w)

Remove twisted from the HTTP server and replace with a
threaded server. Add classes to provide tcp and unix servers
using threads instead of twisted. Remove use of twisted from
the consoles, event server and HTTP resources

Signed-off-by: Mike Wray <mike.wray@hp.com>
author mjw@wray-m-3.hpl.hp.com
date Thu Apr 21 13:25:07 2005 +0000 (2005-04-21)
parents
children 16efdf7bbd57
line source
1 import sys
2 import socket
3 import types
5 from connection import *
6 from protocol import *
8 class TCPServerConnection(SocketServerConnection):
9 pass
11 class TCPListener(SocketListener):
13 def __init__(self, port, factory, backlog=None, interface=''):
14 SocketListener.__init__(self, factory, backlog=backlog)
15 self.port = port
16 self.interface = interface
18 def createSocket(self):
19 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
20 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
21 addr = (self.interface, self.port)
22 sock.bind(addr)
23 return sock
25 def acceptConnection(self, sock, protocol, addr):
26 return TCPServerConnection(sock, protocol, addr, self)
28 class TCPClientConnection(SocketClientConnection):
30 def __init__(self, host, port, bindAddress, connector):
31 SocketClientConnection.__init__(self, connector)
32 self.addr = (host, port)
33 self.bindAddress = bindAddress
35 def createSocket(self):
36 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
37 if self.bindAddress is not None:
38 sock.bind(self.bindAddress)
39 return sock
41 class TCPConnector(SocketConnector):
43 def __init__(self, host, port, factory, timeout=None, bindAddress=None):
44 SocketConnector.__init__(self, factory)
45 self.host = host
46 self.port = self.servicePort(port)
47 self.bindAddress = bindAddress
48 self.timeout = timeout
50 def servicePort(self, port):
51 if isinstance(port, types.StringTypes):
52 try:
53 port = socket.getservbyname(port, 'tcp')
54 except socket.error, ex:
55 raise IOError("unknown service: " + ex)
56 return port
58 def getDestination(self):
59 return (self.host, self.port)
61 def connectTransport(self):
62 self.transport = TCPClientConnection(
63 self.host, self.port, self.bindAddress, self)
64 self.transport.connect(self.timeout)
66 def listenTCP(port, factory, interface='', backlog=None):
67 l = TCPListener(port, factory, interface=interface, backlog=backlog)
68 l.startListening()
69 return l
71 def connectTCP(host, port, factory, timeout=None, bindAddress=None):
72 c = TCPConnector(host, port, factory, timeout=timeout, bindAddress=bindAddress)
73 c.connect()
74 return c
76 def main(argv):
77 host = 'localhost'
78 port = 8005
79 if argv[1] == "client":
80 c = connectTCP(host, port, TestClientFactory())
81 print 'client:', c
82 else:
83 s = listenTCP(port, TestServerFactory())
84 print 'server:', s
86 if __name__ == "__main__":
87 main(sys.argv)