本文整理汇总了Python中connection.Connection.is_connected方法的典型用法代码示例。如果您正苦于以下问题:Python Connection.is_connected方法的具体用法?Python Connection.is_connected怎么用?Python Connection.is_connected使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类connection.Connection
的用法示例。
在下文中一共展示了Connection.is_connected方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from connection import Connection [as 别名]
# 或者: from connection.Connection import is_connected [as 别名]
def main():
server_port = 30000
client_port = 30001
protocol_id= 0x99887766
delta_time = 0.25
send_rate = 0.25
time_out = 10
connection = Connection(protocol_id, time_out)
if not connection.start(server_port):
print("Could not start connection on port {}".format(server_port))
return 1
connection.listen()
while 1:
if connection.is_connected():
print('server sending packet')
packet = [c for c in "server to client"]
connection.send_packet(packet)
while 1:
bytes_read, pack = connection.receive_packet(256)
if bytes_read == 0:
break
print("Received packet from client")
connection.update(delta_time)
time.sleep(delta_time)
示例2: main
# 需要导入模块: from connection import Connection [as 别名]
# 或者: from connection.Connection import is_connected [as 别名]
def main():
server_port = 30000
client_port = 30001
protocol_id = 0x99887766
delta_time = 0.25
send_rate = 0.25
time_out = 10
connection = Connection(protocol_id, time_out)
if not connection.start(client_port):
print("Could not start connection on port {}".format(client_port))
return
connection.connect(Address(a=127,b=0,c=0,d=1,port=server_port))
connected = False
while 1:
if not connected and connection.is_connected():
print("Client connected to server")
connected = True
if not connected and connection.connect_failed():
print("Connection failed")
break
packet = [c for c in 'client to server']
sent_bytes = connection.send_packet(packet)
while 1:
bytes_read, pack = connection.receive_packet(256)
if bytes_read == 0:
break
print("Received packet from server")
connection.update(delta_time)
time.sleep(delta_time)
示例3: Client
# 需要导入模块: from connection import Connection [as 别名]
# 或者: from connection.Connection import is_connected [as 别名]
class Client(object):
"""Client object."""
def __init__(self):
"""
Initializes the client object
"""
super(Client, self).__init__()
self.host = None
self.port = '9300'
self.nick = None
self.name = None
self.parser = Parser(self)
self.message_handler = MessageHandler()
self.update_callback = None
self.tcp = Connection(self, 'TCP')
self.udp = Connection(self, 'UDP')
def register_update_callback(self, func):
"""
"""
self.update_callback = func
def set_host(self, data):
"""
Sets the value of a given field.
Arguments:
- `field`:
The field to set the given `value`.
- `value`:
The value to set.
"""
host = data[2]
logging.debug('Host set to {0}'.format(host))
self.host = host
def set_port(self, data):
"""
Sets the value of a given field.
Arguments:
- `field`:
The field to set the given `value`.
- `value`:
The value to set.
"""
port = data[2]
logging.debug('Port set to {0}'.format(port))
self.port = port
def connect(self, data):
"""
Connects to the server if both hostname and username is set.
"""
if self.host is None:
raise Warning(' '.join(['You must set the server\'s',
'hostname and your name before',
'connecting']))
self.nick = data[2].split()[0]
name = ' '.join(data[2].split()[1:])
try:
self.tcp.connect((self.host, int(self.port)))
except socket.error as e:
return self.denied([e.strerror])
self.tcp.send(
bytes('CONNECT: "{0}" "{1}" {2}\r\n'.format(self.nick,
name,
get_git_version()))
)
self.tcp.handle.start()
def pong(self, data):
if not self.tcp.is_connected():
raise Warning('You are not connected!')
num = data[2]
self.tcp.send(bytes('PONG: {0}\r\n'.format(num)))
def join_channel(self, data):
if not self.tcp.is_connected():
raise Warning('You are not connected!')
channel = data[2]
self.tcp.send(bytes('JOIN: {0}\r\n'.format(channel)))
def message(self, data):
"""
Client > Server:
`MSG #Lobby: Hello there!' (Public message in #Lobby. Sender
#.........这里部分代码省略.........