当前位置: 首页>>代码示例>>Python>>正文


Python SelectReactor.connectTCP方法代码示例

本文整理汇总了Python中twisted.internet.selectreactor.SelectReactor.connectTCP方法的典型用法代码示例。如果您正苦于以下问题:Python SelectReactor.connectTCP方法的具体用法?Python SelectReactor.connectTCP怎么用?Python SelectReactor.connectTCP使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在twisted.internet.selectreactor.SelectReactor的用法示例。


在下文中一共展示了SelectReactor.connectTCP方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: ReactorSlaveController

# 需要导入模块: from twisted.internet.selectreactor import SelectReactor [as 别名]
# 或者: from twisted.internet.selectreactor.SelectReactor import connectTCP [as 别名]
class ReactorSlaveController(object):
    def __init__(self):
        self.keepGoing = True
        self.reactor = SelectReactor()
        installReactor(self.reactor)
        connection = self.reactor.connectTCP('localhost', 8000, factory)
        self.reactor.startRunning()
        self.futureCall = None
        self.futureCallTimeout = None
        pygame_test.prepare()
        
    def iterate(self):
        print 'in iterate'
        self.reactor.runUntilCurrent()
        self.reactor.doIteration(0)
        #t2 = self.reactor.timeout()
        #print 'timeout', t2
        #t = self.reactor.running and t2
        #self.reactor.doIteration(t)

    def run(self):
	clock = pygame.time.Clock()
        self.reactor.callLater(20, stupidTest)
        while self.keepGoing:
            timeChange = clock.tick(FRAMES_PER_SECOND)
            if self.futureCall:
                self.futureCallTimeout -= timeChange
                print 'future call in', self.futureCallTimeout
                if self.futureCallTimeout <= 0:
                    self.futureCall()
                    self.futureCallTimeout = None
                    self.futureCall= None
            retval = pygame_test.iterate()
            if retval == False:
                thingInControl.stop()
            self.iterate()

    def stop(self):
        print 'stopping'
        self.reactor.stop()
        self.keepGoing = False

    def callLater(self, when, fn):
        self.futureCallTimeout = when*1000
        self.futureCall = fn
        print 'future call in', self.futureCallTimeout
开发者ID:ClashTeak,项目名称:writing_games_tutorial,代码行数:48,代码来源:multi_controller_test.py

示例2: NetworkServerView

# 需要导入模块: from twisted.internet.selectreactor import SelectReactor [as 别名]
# 或者: from twisted.internet.selectreactor.SelectReactor import connectTCP [as 别名]
class NetworkServerView(pb.Root):
    """We SEND events to the server through this object"""
    STATE_PREPARING = 0
    STATE_CONNECTING = 1
    STATE_CONNECTED = 2
    STATE_DISCONNECTING = 3
    STATE_DISCONNECTED = 4

    #----------------------------------------------------------------------
    def __init__(self, evManager, sharedObjectRegistry):
        self.evManager = evManager
        self.evManager.RegisterListener( self )

        self.pbClientFactory = pb.PBClientFactory()
        self.state = NetworkServerView.STATE_PREPARING
        self.reactor = None
        self.server = None

        self.sharedObjs = sharedObjectRegistry

    #----------------------------------------------------------------------
    def AttemptConnection(self):
        print "attempting a connection to", serverHost, serverPort
        self.state = NetworkServerView.STATE_CONNECTING
        if self.reactor:
            self.reactor.stop()
            self.PumpReactor()
        else:
            self.reactor = SelectReactor()
            installReactor(self.reactor)
        connection = self.reactor.connectTCP(serverHost, serverPort,
                                             self.pbClientFactory)
        # TODO: make this anonymous login()
        #deferred = self.pbClientFactory.login(credentials.Anonymous())
        userCred = credentials.UsernamePassword(avatarID, 'pass1')
        controller = NetworkServerController( self.evManager )
        deferred = self.pbClientFactory.login(userCred, client=controller)
        deferred.addCallback(self.Connected)
        deferred.addErrback(self.ConnectFailed)
        self.reactor.startRunning()

    #----------------------------------------------------------------------
    def Disconnect(self):
        print "disconnecting"
        if not self.reactor:
            return
        print 'stopping the reactor'
        self.reactor.stop()
        self.PumpReactor()
        self.state = NetworkServerView.STATE_DISCONNECTING

    #----------------------------------------------------------------------
    def Connected(self, server):
        print "CONNECTED"
        self.server = server
        self.state = NetworkServerView.STATE_CONNECTED
        ev = ServerConnectEvent( server )
        self.evManager.Post( ev )

    #----------------------------------------------------------------------
    def ConnectFailed(self, server):
        print "CONNECTION FAILED"
        print server
        print 'quitting'
        self.evManager.Post( QuitEvent() )
        #self.state = NetworkServerView.STATE_PREPARING
        self.state = NetworkServerView.STATE_DISCONNECTED

    #----------------------------------------------------------------------
    def PumpReactor(self):
        self.reactor.runUntilCurrent()
        self.reactor.doIteration(0)

    #----------------------------------------------------------------------
    def Notify(self, event):
        NSV = NetworkServerView
        if isinstance( event, TickEvent ):
            if self.state == NSV.STATE_PREPARING:
                self.AttemptConnection()
            elif self.state in [NSV.STATE_CONNECTED,
                                NSV.STATE_DISCONNECTING,
                                NSV.STATE_CONNECTING]:
                self.PumpReactor()
            return

        if isinstance( event, QuitEvent ):
            self.Disconnect()
            return

        ev = event
        if not isinstance( event, pb.Copyable ):
            evName = event.__class__.__name__
            copyableClsName = "Copyable"+evName
            if not hasattr( network, copyableClsName ):
                return
            copyableClass = getattr( network, copyableClsName )
            #NOTE, never even construct an instance of an event that
            # is serverToClient, as a side effect is often adding a
            # key to the registry with the local id().
            if copyableClass not in network.clientToServerEvents:
#.........这里部分代码省略.........
开发者ID:ClashTeak,项目名称:writing_games_tutorial,代码行数:103,代码来源:client.py

示例3: NetworkServerView

# 需要导入模块: from twisted.internet.selectreactor import SelectReactor [as 别名]
# 或者: from twisted.internet.selectreactor.SelectReactor import connectTCP [as 别名]
class NetworkServerView(pb.Root):
    """We SEND events to the server through this object"""
    STATE_PREPARING = 0
    STATE_CONNECTING = 1
    STATE_CONNECTED = 2

    def __init__(self, evm, shared_obj_registry):
        self.evm = evm
        self.evm.register(self)

        self.pbClientFactory = pb.PBClientFactory()
        
        self.reactor = None
        self.server = None

        self.shared_objs = shared_obj_registry
        self.state = NetworkServerView.STATE_PREPARING
 
    def connected(self, server):
        print 'Connection to %s %s successful' %SERVERHOST, SERVERPORT

        self.state = NetworkServerView.STATE_CONNECTED
        
        self.server = server
        self.evm.post(ev.ServerConnected(server))
        
    def connection_failed(self, error):
        print "Connection failed with error ", error

    def attempt_connection(self):
        print "Attempting connection to", SERVERHOST, SERVERPORT
        
        self.state = NetworkServerView.STATE_CONNECTING
        
        if self.reactor:
            self.reactor.stop()
            self.pump_reactor()
        else:
            self.reactor = SelectReactor()
            installReactor(self.reactor)
        
        connection = self.reactor.connectTCP(SERVERHOST, SERVERPORT,
                                             self.pbClientFactory)
        deferred = self.pbClientFactory.getRootObject()        
        deferred.addCallback(self.connected)
        deferred.addErrback(self.connection_failed)
        self.reactor.run()
        
    def pump_reactor(self):
        self.reactor.runUntilCurrent()
        self.reactor.doIteration(0)

        
    def disconnect(self):
        print "disconnecting"

    def notify(self, evt):
        if evt.id == 'Tick':
            if self.state == NetworkServerView.STATE_PREPARING:
                self.attempt_connection()
            elif self.state in [NetworkServerView.STATE_CONNECTING]:
                self.pump_reactor()
开发者ID:dmalves,项目名称:galgrav,代码行数:64,代码来源:client.py

示例4: SelectReactor

# 需要导入模块: from twisted.internet.selectreactor import SelectReactor [as 别名]
# 或者: from twisted.internet.selectreactor.SelectReactor import connectTCP [as 别名]
from twisted.internet.selectreactor import SelectReactor
from twisted.internet.protocol import Protocol, ClientFactory
reactor = SelectReactor()
protocol = Protocol()
class QuickDisconnectedProtocol(Protocol):
    def connectionMade(self):
        print "Connected to %s."%self.transport.getPeer().host
        self.transport.loseConnection()

class BasicClientFactory(ClientFactory):
    protocol = QuickDisconnectedProtocol
    def clientConnectionLost(self, connector, reason):
        print 'Lost connection:%s'%reason.getErrorMessage()
        reactor.stop()
    def clientConnectionFailed(self,connector,reason):
        print 'Connection failed: %s'%reason.getErrorMessage()
        reactor.stop()

reactor.connectTCP('www.google.com',80,BasicClientFactory())
reactor.run()
开发者ID:wwq0327,项目名称:lovepy,代码行数:22,代码来源:twtcp.py

示例5: NetworkServerView

# 需要导入模块: from twisted.internet.selectreactor import SelectReactor [as 别名]
# 或者: from twisted.internet.selectreactor.SelectReactor import connectTCP [as 别名]
class NetworkServerView(pb.Root):
	"""We SEND events to the server through this object"""
	STATE_PREPARING = 0
	STATE_CONNECTING = 1
	STATE_CONNECTED = 2
	STATE_DISCONNECTING = 3
	STATE_DISCONNECTED = 4

	#----------------------------------------------------------------------
	def __init__(self, evManager, sharedObjectRegistry):
		self.evManager = evManager
		self.evManager.RegisterListener( self )

		self.pbClientFactory = pb.PBClientFactory()
		self.state = NetworkServerView.STATE_PREPARING
		self.reactor = None
		self.server = None

		self.sharedObjs = sharedObjectRegistry

	#----------------------------------------------------------------------
	def AttemptConnection(self):
		print "attempting a connection to", serverHost, serverPort
		self.state = NetworkServerView.STATE_CONNECTING
		if self.reactor:
			self.reactor.stop()
			self.PumpReactor()
		else:
			self.reactor = SelectReactor()
			installReactor(self.reactor)
		connection = self.reactor.connectTCP(serverHost, serverPort,
		                                     self.pbClientFactory)
		deferred = self.pbClientFactory.getRootObject()
		deferred.addCallback(self.Connected)
		deferred.addErrback(self.ConnectFailed)
		self.reactor.startRunning()

	#----------------------------------------------------------------------
	def Disconnect(self):
		print "disconnecting"
		if not self.reactor:
			return
		print 'stopping the reactor'
		self.reactor.stop()
                self.PumpReactor()
		self.state = NetworkServerView.STATE_DISCONNECTING

	#----------------------------------------------------------------------
	def Connected(self, server):
		print "CONNECTED"
		self.server = server
		self.state = NetworkServerView.STATE_CONNECTED
		ev = ServerConnectEvent( server )
		self.evManager.Post( ev )

	#----------------------------------------------------------------------
	def ConnectFailed(self, server):
		print "CONNECTION FAILED"
		#self.state = NetworkServerView.STATE_PREPARING
		self.state = NetworkServerView.STATE_DISCONNECTED

	#----------------------------------------------------------------------
	def PumpReactor(self):
		self.reactor.runUntilCurrent()
		self.reactor.doIteration(0)

	#----------------------------------------------------------------------
	def Notify(self, event):
		NSV = NetworkServerView
		if isinstance( event, TickEvent ):
			if self.state == NSV.STATE_PREPARING:
				self.AttemptConnection()
			elif self.state in [NSV.STATE_CONNECTED,
			                    NSV.STATE_DISCONNECTING,
			                    NSV.STATE_CONNECTING]:
				self.PumpReactor()
			return

		if isinstance( event, QuitEvent ):
			self.Disconnect()
			return

		ev = event
		if not isinstance( event, pb.Copyable ):
			evName = event.__class__.__name__
			copyableClsName = "Copyable"+evName
			if not hasattr( network, copyableClsName ):
				return
			copyableClass = getattr( network, copyableClsName )
			ev = copyableClass( event, self.sharedObjs )

		if ev.__class__ not in network.clientToServerEvents:
			#print "CLIENT NOT SENDING: " +str(ev)
			return
			
		if self.server:
			print " ====   Client sending", str(ev)
			remoteCall = self.server.callRemote("EventOverNetwork",
			                                    ev)
		else:
#.........这里部分代码省略.........
开发者ID:ClashTeak,项目名称:writing_games_tutorial,代码行数:103,代码来源:client.py


注:本文中的twisted.internet.selectreactor.SelectReactor.connectTCP方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。