OLD | NEW |
| (Empty) |
1 # -*- test-case-name: twisted.test.test_strports -*- | |
2 | |
3 # Copyright (c) 2001-2004 Twisted Matrix Laboratories. | |
4 # See LICENSE for details. | |
5 | |
6 # | |
7 """ | |
8 Port description language | |
9 | |
10 This module implements a description mini-language for ports, and provides | |
11 functions to parse it and to use it to directly construct appropriate | |
12 network server services or to directly listen on them. | |
13 | |
14 Here are some examples:: | |
15 >>> s=service("80", server.Site()) | |
16 >>> s=service("tcp:80", server.Site()) | |
17 >>> s=service("tcp:80:interface=127.0.0.1", server.Site()) | |
18 >>> s=service("ssl:443", server.Site()) | |
19 >>> s=service("ssl:443:privateKey=mykey.pem", server.Site()) | |
20 >>> s=service("ssl:443:privateKey=mykey.pem:certKey=cert.pem", server.Site()) | |
21 >>> s=service("unix:/var/run/finger", FingerFactory()) | |
22 >>> s=service("unix:/var/run/finger:mode=660", FingerFactory()) | |
23 >>> p=listen("80", server.Site()) | |
24 >>> p=listen("tcp:80", server.Site()) | |
25 >>> p=listen("tcp:80:interface=127.0.0.1", server.Site()) | |
26 >>> p=listen("ssl:443", server.Site()) | |
27 >>> p=listen("ssl:443:privateKey=mykey.pem", server.Site()) | |
28 >>> p=listen("ssl:443:privateKey=mykey.pem:certKey=cert.pem", server.Site()) | |
29 >>> p=listen("unix:/var/run/finger", FingerFactory()) | |
30 >>> p=listen("unix:/var/run/finger:mode=660", FingerFactory()) | |
31 | |
32 See specific function documentation for more information. | |
33 | |
34 Maintainer: U{Moshe Zadka<mailto:moshez@twistedmatrix.com>} | |
35 """ | |
36 from __future__ import generators | |
37 | |
38 def _parseTCP(factory, port, interface="", backlog=50): | |
39 return (int(port), factory), {'interface': interface, | |
40 'backlog': int(backlog)} | |
41 | |
42 def _parseUNIX(factory, address, mode='666', backlog=50): | |
43 return (address, factory), {'mode': int(mode, 8), 'backlog': int(backlog)} | |
44 | |
45 def _parseSSL(factory, port, privateKey="server.pem", certKey=None, | |
46 sslmethod=None, interface='', backlog=50): | |
47 from twisted.internet import ssl | |
48 if certKey is None: | |
49 certKey = privateKey | |
50 kw = {} | |
51 if sslmethod is not None: | |
52 kw['sslmethod'] = getattr(ssl.SSL, sslmethod) | |
53 cf = ssl.DefaultOpenSSLContextFactory(privateKey, certKey, **kw) | |
54 return ((int(port), factory, cf), | |
55 {'interface': interface, 'backlog': int(backlog)}) | |
56 | |
57 _funcs = {"tcp": _parseTCP, | |
58 "unix": _parseUNIX, | |
59 "ssl": _parseSSL} | |
60 | |
61 _OP, _STRING = range(2) | |
62 def _tokenize(description): | |
63 current = '' | |
64 ops = ':=' | |
65 nextOps = {':': ':=', '=': ':'} | |
66 description = iter(description) | |
67 for n in description: | |
68 if n in ops: | |
69 yield _STRING, current | |
70 yield _OP, n | |
71 current = '' | |
72 ops = nextOps[n] | |
73 elif n=='\\': | |
74 current += description.next() | |
75 else: | |
76 current += n | |
77 yield _STRING, current | |
78 | |
79 def _parse(description): | |
80 args, kw = [], {} | |
81 def add(sofar): | |
82 if len(sofar)==1: | |
83 args.append(sofar[0]) | |
84 else: | |
85 kw[sofar[0]] = sofar[1] | |
86 sofar = () | |
87 for (type, value) in _tokenize(description): | |
88 if type is _STRING: | |
89 sofar += (value,) | |
90 elif value==':': | |
91 add(sofar) | |
92 sofar = () | |
93 add(sofar) | |
94 return args, kw | |
95 | |
96 def parse(description, factory, default=None): | |
97 """ | |
98 Parse the description of a reliable virtual circuit server (that is, a | |
99 TCP port, a UNIX domain socket or an SSL port) and return the data | |
100 necessary to call the reactor methods to listen on the given socket with | |
101 the given factory. | |
102 | |
103 An argument with no colons means a default port. Usually the default | |
104 type is C{tcp}, but passing a non-C{None} value as C{default} will set | |
105 that as the default. Otherwise, it is a colon-separated string. The | |
106 first part means the type -- currently, it can only be ssl, unix or tcp. | |
107 After that, comes a list of arguments. Arguments can be positional or | |
108 keyword, and can be mixed. Keyword arguments are indicated by | |
109 C{'name=value'}. If a value is supposed to contain a C{':'}, a C{'='} or | |
110 a C{'\\'}, escape it with a C{'\\'}. | |
111 | |
112 For TCP, the arguments are the port (port number) and, optionally the | |
113 interface (interface on which to listen) and backlog (how many clients | |
114 to keep in the backlog). | |
115 | |
116 For UNIX domain sockets, the arguments are address (the file name of the | |
117 socket) and optionally the mode (the mode bits of the file, as an octal | |
118 number) and the backlog (how many clients to keep in the backlog). | |
119 | |
120 For SSL sockets, the arguments are the port (port number) and, | |
121 optionally, the privateKey (file in which the private key is in), | |
122 certKey (file in which the certification is in), sslmethod (the name of | |
123 the SSL method to allow), the interface (interface on which to listen) | |
124 and the backlog (how many clients to keep in the backlog). | |
125 | |
126 @type description: C{str} | |
127 @type factory: L{twisted.internet.interfaces.IProtocolFactory} | |
128 @type default: C{str} or C{None} | |
129 @rtype: C{tuple} | |
130 @return: a tuple of string, tuple and dictionary. The string is the name | |
131 of the method (sans C{'listen'}) to call, and the tuple and dictionary | |
132 are the arguments and keyword arguments to the method. | |
133 @raises ValueError: if the string is formatted incorrectly. | |
134 @raises KeyError: if the type is other than unix, ssl or tcp. | |
135 """ | |
136 args, kw = _parse(description) | |
137 if not args or (len(args)==1 and not kw): | |
138 args[0:0] = [default or 'tcp'] | |
139 return (args[0].upper(),)+_funcs[args[0]](factory, *args[1:], **kw) | |
140 | |
141 def service(description, factory, default=None): | |
142 """Return the service corresponding to a description | |
143 | |
144 @type description: C{str} | |
145 @type factory: L{twisted.internet.interfaces.IProtocolFactory} | |
146 @type default: C{str} or C{None} | |
147 @rtype: C{twisted.application.service.IService} | |
148 @return: the service corresponding to a description of a reliable | |
149 virtual circuit server. | |
150 | |
151 See the documentation of the C{parse} function for description | |
152 of the semantics of the arguments. | |
153 """ | |
154 from twisted.application import internet | |
155 name, args, kw = parse(description, factory, default) | |
156 return getattr(internet, name+'Server')(*args, **kw) | |
157 | |
158 def listen(description, factory, default=None): | |
159 """Listen on a port corresponding to a description | |
160 | |
161 @type description: C{str} | |
162 @type factory: L{twisted.internet.interfaces.IProtocolFactory} | |
163 @type default: C{str} or C{None} | |
164 @rtype: C{twisted.internet.interfaces.IListeningPort} | |
165 @return: the port corresponding to a description of a reliable | |
166 virtual circuit server. | |
167 | |
168 See the documentation of the C{parse} function for description | |
169 of the semantics of the arguments. | |
170 """ | |
171 from twisted.internet import reactor | |
172 name, args, kw = parse(description, factory, default) | |
173 return getattr(reactor, 'listen'+name)(*args, **kw) | |
174 | |
175 __all__ = ['parse', 'service', 'listen'] | |
OLD | NEW |