[Twisted-Python] Information on new endpoints API?
exarkun at twistedmatrix.com
exarkun at twistedmatrix.com
Tue Jun 14 14:37:45 EDT 2011
On 07:37 am, albert.brandl at weiermayer.com wrote:
>On Sun, Jun 12, 2011 at 01:22:35PM -0000, exarkun at twistedmatrix.com
>wrote:
>>`twisted.internet.endpoints.serverFromString` is one way you might get
>>this endpoint (in particular, I would emphasize that serverFromString
>>is
>>really only meant to construct an endpoint if you *have* to start with
>>a
>>string - as you do when you are reading a configuration file).
>
>This works also quite well when you have different kinds of connection
>in the testing and production environment. We read data from a GPS
>device. For testing purposes, we want to feed the NMEA telegrams into
>the server using a TCP connection, but the device is connected to the
>server on a serial port.
This is cool, I'm glad endpoints are working out for you. :)
>We've defined a constant GPS_PORT as "SERIAL:/dev/ttyS2:baudrate=4800"
>for the production environment and "tcp:1049" for the test environment
To be clear, though, it sounds like you're defining GPS_PORT in a .py
file, and that's more or less what I was speaking against. :) Instead,
I would define GPS_PORT as either SerialEndpoint(reactor, "/dev/ttyS2",
baudrate=4800) or TCP4ServerEndpoint(reactor, 1049).
The rest of your code can still be indifferent to which of these is in
play (and you can have a service for either of these using
StreamServerEndpointService). If you misspell something or leave off a
required argument, though, you're more likely to get an exception that
points more directly at the problem.
Put another way, I suggest writing this:
if debug_mode():
gps_port = TCP4ServerEndpoint(reactor, 1049)
else:
gps_port = SerialEndpoint(reactor, "/dev/ttyS2", baudrate=4800)
...
gps_service = StreamServerEndpointService(gps_port, gps_factory)
And save the "tcp:1049"-style strings for your .ini files where you
cannot write it that way.
Jean-Paul
More information about the Twisted-Python
mailing list