本文整理匯總了Python中transport.Transport.write方法的典型用法代碼示例。如果您正苦於以下問題:Python Transport.write方法的具體用法?Python Transport.write怎麽用?Python Transport.write使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類transport.Transport
的用法示例。
在下文中一共展示了Transport.write方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: IRCClient
# 需要導入模塊: from transport import Transport [as 別名]
# 或者: from transport.Transport import write [as 別名]
class IRCClient(object):
COMMON_SERVICES = [IRCRules]
def __init__(self, **params):
self.params = params
self.transport = Transport()
self.reader = DelimitedReader("\r\n")
self.reader.add_callback(self.new_line)
self._services = []
for t in type(self).COMMON_SERVICES:
self.add_service(t())
for i in self.services():
self.add_service(i)
self.lock_many = threading.Lock()
self.lock_single = threading.Lock()
self.common_state = {"irc": self}
# This is not persistent and transmitted as the sender for
# events like IRC commands
def update_common_state(self, newstate):
self.common_state.update(newstate)
def get_common_state(self):
return self.common_state
def services(self):
"""
Should return an Iterable of services initialized for the current
instance.
You should override this when subclassing IRCClient.
"""
return []
def loop(self):
# Automates connect()/update() in a separate thread.
self.looping = True
self.state = load_state()
threading.Thread(target = self._loop).start()
def _loop(self):
self.connect()
while self.looping:
time.sleep(0.01)
self.update()
def stop(self):
dump_state(self.state)
self.looping = False
def connect(self):
host, post = self.cfgget("host"), self.cfgget("port")
self.transport.connect((host, post,))
for i in self._services:
i.handle(self, messages.JustConnected())
def update(self):
try:
self.reader.update(self.transport)
except TransportError:
self.stop()
def new_line(self, line):
try:
parsed = primitive.parse(line)
print "Received line: %s" % line.strip()
except primitive.UnknownMessageError:
print "Received (but could not understand) line: %s" % line
return
for i in self._services:
i.handle(self, parsed)
def add_service(self, s):
self._services.append(s)
s.patch(self)
def cfgget(self, name, default = NOPARAM):
"""
Gets the given configuration parameter. The following order of
precedence is used to return the parameter in order to deal with
cases where it is unspecified:
1. self.params[name]
2. default
3. default_params[name]
4. None
"""
try:
return self.params[name]
except KeyError:
pass
if default != NOPARAM: return default
try:
return default_params[name]
except KeyError:
pass
return None
def cfgset(self, name, value):
self.params[name] = value
def send(self, message):
"""
Sends a single message.
This method is thread-safe: however, it's probably best to use sendmany
if you're attempting to send a series of messages at once, so that
unrelated send-multiple-messages requests don't intersect
accidentally.
"""
with self.lock_single:
out = message.write()
print "Sending line: %s" % out
#.........這裏部分代碼省略.........