ia64/xen-unstable

view tools/python/xen/web/tcp.py @ 6538:84ee014ebd41

Merge xen-vtx-unstable.hg
author adsharma@los-vmm.sc.intel.com
date Wed Aug 17 12:34:38 2005 -0800 (2005-08-17)
parents 23979fb12c49 fb619858dfa9
children 99914b54f7bf
line source
1 #============================================================================
2 # This library is free software; you can redistribute it and/or
3 # modify it under the terms of version 2.1 of the GNU Lesser General Public
4 # License as published by the Free Software Foundation.
5 #
6 # This library is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 # Lesser General Public License for more details.
10 #
11 # You should have received a copy of the GNU Lesser General Public
12 # License along with this library; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 #============================================================================
15 # Copyright (C) 2005 Mike Wray <mike.wray@hp.com>
16 #============================================================================
18 import sys
19 import socket
20 import types
22 from connection import *
23 from protocol import *
25 class TCPServerConnection(SocketServerConnection):
26 pass
28 class TCPListener(SocketListener):
30 def __init__(self, port, factory, backlog=None, interface=''):
31 SocketListener.__init__(self, factory, backlog=backlog)
32 self.port = port
33 self.interface = interface
35 def createSocket(self):
36 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
37 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
38 addr = (self.interface, self.port)
39 sock.bind(addr)
40 return sock
42 def acceptConnection(self, sock, protocol, addr):
43 return TCPServerConnection(sock, protocol, addr, self)
45 class TCPClientConnection(SocketClientConnection):
47 def __init__(self, host, port, bindAddress, connector):
48 SocketClientConnection.__init__(self, connector)
49 self.addr = (host, port)
50 self.bindAddress = bindAddress
52 def createSocket(self):
53 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
54 if self.bindAddress is not None:
55 sock.bind(self.bindAddress)
56 return sock
58 class TCPConnector(SocketConnector):
60 def __init__(self, host, port, factory, timeout=None, bindAddress=None):
61 SocketConnector.__init__(self, factory)
62 self.host = host
63 self.port = self.servicePort(port)
64 self.bindAddress = bindAddress
65 self.timeout = timeout
67 def servicePort(self, port):
68 if isinstance(port, types.StringTypes):
69 try:
70 port = socket.getservbyname(port, 'tcp')
71 except socket.error, ex:
72 raise IOError("unknown service: " + ex)
73 return port
75 def getDestination(self):
76 return (self.host, self.port)
78 def connectTransport(self):
79 self.transport = TCPClientConnection(
80 self.host, self.port, self.bindAddress, self)
81 self.transport.connect(self.timeout)
83 def listenTCP(port, factory, interface='', backlog=None):
84 l = TCPListener(port, factory, interface=interface, backlog=backlog)
85 l.startListening()
86 return l
88 def SetCloExec(SocketListener):
89 SocketListener.SetCloExec()
91 def connectTCP(host, port, factory, timeout=None, bindAddress=None):
92 c = TCPConnector(host, port, factory, timeout=timeout, bindAddress=bindAddress)
93 c.connect()
94 return c
96 def main(argv):
97 host = 'localhost'
98 port = 8005
99 if argv[1] == "client":
100 c = connectTCP(host, port, TestClientFactory())
101 print 'client:', c
102 else:
103 s = listenTCP(port, TestServerFactory())
104 print 'server:', s
106 if __name__ == "__main__":
107 main(sys.argv)