def __install_named_reactor(reactor_name, reactor_specific=None): """Setup a proper Twisted reactor, given its name. @precondition: reactor_name in SUPPORTED_REACTOR_NAMES @param reactor_specific: some arguments which are specific for a particular reactor (a bit hacky, yes). If C{reactor_name} == 'qt4': contains particular C{QCoreApplication} subclass to be used for primary application creation. May be C{None}. @raises ImportError: if the reactor cannot be installed. @raises NotImplementedError: if such reactor name is unsupported. """ logger.debug('Attempting to install %s reactor...', reactor_name) if reactor_name == 'epoll': from twisted.internet import epollreactor epollreactor.install() elif reactor_name == 'kqueue': from txkqr import kqreactor_ex kqreactor_ex.install() elif reactor_name == 'qt4': qapp_class = reactor_specific if qapp_class is not None: app = qapp_class(sys.argv) from contrib.qt4reactor import qt4reactor qt4reactor.install() elif reactor_name == 'win32': from twisted.internet import win32eventreactor win32eventreactor.install() elif reactor_name == 'zmq': from zmqr import zmqreactor zmqreactor.install() else: logger.debug('Cannot install %s reactor, using default', reactor_name) raise NotImplementedError('Unsupported reactor {!r}' .format(reactor_name)) logger.debug('Installed %s reactor', reactor_name)
#!/usr/bin/python """ GPSTest is a simple example using the SerialPort transport and the NMEA 0183 and Rockwell Zodiac GPS protocols to display fix data as it is received from the device. """ from twisted.python import log, usage import sys if sys.platform == 'win32': from twisted.internet import win32eventreactor win32eventreactor.install() class GPSFixLogger: def handle_fix(self, *args): """handle_fix gets called whenever either rockwell.Zodiac or nmea.NMEAReceiver receives and decodes fix data. Generally, GPS receivers will report a fix at 1hz. Implementing only this method is sufficient for most purposes unless tracking of ground speed, course, utc date, or detailed satellite information is necessary. For example, plotting a map from MapQuest or a similar service only requires longitude and latitude. """ log.msg('fix:\n' + '\n'.join(map(lambda n: ' %s = %s' % tuple(n), zip(('utc', 'lon', 'lat', 'fix', 'sat', 'hdp', 'alt', 'geo', 'dgp'), map(repr, args))))) class GPSOptions(usage.Options): optFlags = [ ['zodiac', 'z', 'Use Rockwell Zodiac (DeLorme Earthmate) [default: NMEA 0183]'], ] optParameters = [ ['outfile', 'o', None, 'Logfile [default: sys.stdout]'], ['baudrate', 'b', None, 'Serial baudrate [default: 4800 for NMEA, 9600 for Zodiac]'],
import sys, socket, struct, time from twisted.internet import reactor hostName = 'bach.ese.wustl.edu' host = None defaultTwistedServerPort = 53335 prevTime = 0.0 accumTime = 0.0 # use win32 reactor if applicable if sys.platform == 'win32': from twisted.internet import win32eventreactor win32eventreactor.install() # find hostname def findHost(): addr = socket.gethostbyname(hostName) return addr ######################################################3 class SocketClientProtocol(LineOnlyReceiver): # after int prefix and other framing are removed: def lineReceived(self, line):