本文整理汇总了Python中connection.Connection.read方法的典型用法代码示例。如果您正苦于以下问题:Python Connection.read方法的具体用法?Python Connection.read怎么用?Python Connection.read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类connection.Connection
的用法示例。
在下文中一共展示了Connection.read方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle
# 需要导入模块: from connection import Connection [as 别名]
# 或者: from connection.Connection import read [as 别名]
def handle(self):
connection = Connection(self.request)
command = connection.read(1)
if command == "l":
list(connection)
else:
sys.stderr.write("bad command: " + repr(command) + "\n")
connection.write(b"b")
connection.write_string("you suck")
示例2: Bot
# 需要导入模块: from connection import Connection [as 别名]
# 或者: from connection.Connection import read [as 别名]
class Bot(object):
ping_pattern = re.compile('^PING (?P<payload>.*)')
chanmsg_pattern = re.compile(':(?P<nick>.*?)!\S+\s+?PRIVMSG\s+#(?P<channel>[-\w]+)\s+:(?P<message>[^\n\r]+)')
def __init__(self, server, ident, channel, path):
self._dispatch_table = (
(self.ping_pattern, self.handle_ping),
(self.chanmsg_pattern, self.handle_chanmsg))
self._logger = Logging(path)
self.server = server
self.ident = ident
self.channel = channel
self.start()
def start(self):
self._connection = Connection(self.server)
self.register_connection(self.ident)
self.join_channel(self.channel)
def loop(self):
while True:
try:
line = self._connection.read()
except socket.error as se:
trackeback.print_exc()
print "Caught exception. Will reconnect."
del(self._connection)
time.sleep(60)
self.start()
continue
for pattern, handler in self._dispatch_table:
match = pattern.match(line)
if match:
handler(**match.groupdict())
def handle_ping(self, payload):
self._connection.send("PONG " + payload)
def handle_chanmsg(self, nick, channel, message):
self._logger.write(nick + ": " + message)
def register_connection(self, ident):
nick, passw = ident
self._connection.send("PASS " + passw)
self._connection.send("NICK " + nick)
self._connection.send("USER " + nick + " 0 * :" + nick)
def join_channel(self, channel):
chan, passw = channel
self._connection.send("JOIN " + chan + " " + passw)
示例3: Node
# 需要导入模块: from connection import Connection [as 别名]
# 或者: from connection.Connection import read [as 别名]
class Node(object):
def __init__(self, syncObj, nodeAddr):
self.__syncObj = weakref.ref(syncObj)
self.__nodeAddr = nodeAddr
self.__ip = syncObj._getResolver().resolve(nodeAddr.split(':')[0])
self.__port = int(nodeAddr.split(':')[1])
self.__poller = syncObj._getPoller()
self.__conn = Connection(socket=None, timeout=syncObj._getConf().connectionTimeout)
self.__shouldConnect = syncObj._getSelfNodeAddr() > nodeAddr
self.__lastConnectAttemptTime = 0
self.__lastPingTime = 0
self.__status = NODE_STATUS.DISCONNECTED
def __del__(self):
self.__conn = None
self.__poller = None
def onPartnerConnected(self, conn):
self.__conn = conn
self.__status = NODE_STATUS.CONNECTED
self.__poller.subscribe(self.__conn.fileno(),
self.__processConnection,
POLL_EVENT_TYPE.READ | POLL_EVENT_TYPE.WRITE | POLL_EVENT_TYPE.ERROR)
def getStatus(self):
return self.__status
def isConnected(self):
return self.__status == NODE_STATUS.CONNECTED
def getAddress(self):
return self.__nodeAddr
def getSendBufferSize(self):
return self.__conn.getSendBufferSize()
def send(self, message):
if self.__status != NODE_STATUS.CONNECTED:
return False
self.__conn.send(message)
if self.__conn.isDisconnected():
self.__status = NODE_STATUS.DISCONNECTED
self.__poller.unsubscribe(self.__conn.fileno())
self.__conn.close()
return False
return True
def connectIfRequired(self):
if not self.__shouldConnect:
return
if self.__status != NODE_STATUS.DISCONNECTED:
return
if time.time() - self.__lastConnectAttemptTime < self.__syncObj()._getConf().connectionRetryTime:
return
self.__status = NODE_STATUS.CONNECTING
self.__lastConnectAttemptTime = time.time()
if not self.__conn.connect(self.__ip, self.__port):
self.__status = NODE_STATUS.DISCONNECTED
return
self.__poller.subscribe(self.__conn.fileno(),
self.__processConnection,
POLL_EVENT_TYPE.READ | POLL_EVENT_TYPE.WRITE | POLL_EVENT_TYPE.ERROR)
def __processConnection(self, descr, eventType):
if descr != self.__conn.fileno():
self.__poller.unsubscribe(descr)
return
isError = False
if eventType & POLL_EVENT_TYPE.ERROR:
isError = True
if eventType & POLL_EVENT_TYPE.READ or eventType & POLL_EVENT_TYPE.WRITE:
if self.__conn.socket().getsockopt(socket.SOL_SOCKET, socket.SO_ERROR):
isError = True
else:
if self.__status == NODE_STATUS.CONNECTING:
self.__conn.send(self.__syncObj()._getSelfNodeAddr())
self.__status = NODE_STATUS.CONNECTED
if isError or self.__conn.isDisconnected():
self.__status = NODE_STATUS.DISCONNECTED
self.__conn.close()
self.__poller.unsubscribe(descr)
return
if eventType & POLL_EVENT_TYPE.WRITE:
if self.__status == NODE_STATUS.CONNECTING:
self.__conn.send(self.__syncObj()._getSelfNodeAddr())
self.__status = NODE_STATUS.CONNECTED
self.__conn.trySendBuffer()
event = POLL_EVENT_TYPE.READ | POLL_EVENT_TYPE.ERROR
if self.__conn.getSendBufferSize() > 0:
event |= POLL_EVENT_TYPE.WRITE
if not self.__conn.isDisconnected():
self.__poller.subscribe(descr, self.__processConnection, event)
if eventType & POLL_EVENT_TYPE.READ:
if self.__conn.read():
#.........这里部分代码省略.........
示例4: Sensor
# 需要导入模块: from connection import Connection [as 别名]
# 或者: from connection.Connection import read [as 别名]
#.........这里部分代码省略.........
Examples:
>>> Sensor('/') == Sensor('/1F.440701000000')
False
>>> Sensor('/') == Sensor('/uncached')
True
"""
# print 'Sensor.__eq__(%s)' % str(other)
return self._path == other._path
def __hash__(self):
"""
Return a hash for the Sensor object's name. This allows
Sensors to be used better in sets.Set.
"""
# print 'Sensor.__hash__'
return hash(self._path)
def __getattr__(self, name):
"""
Retreive an attribute from the sensor. __getattr__ is called
only if the named item doesn't exist in the Sensor's
namespace. If it's not in the namespace, look for the attribute
on the physical sensor.
Usage:
s = ownet.Sensor('/1F.5D0B01000000')
print s.family, s.PIO_0
will result in the family and PIO.0 values being read from the
sensor and printed. In this example, the family would be 1F
and thr PIO.0 might be 1.
"""
try:
# print 'Sensor.__getattr__(%s)' % name
attr = self._connection.read(object.__getattribute__(self, "_attrs")[name])
except:
raise AttributeError, name
return attr
def __setattr__(self, name, value):
"""
Set the value of a sensor attribute. This is accomplished by
first determining if the physical sensor has the named
attribute. If it does, then the value is written to the
name. Otherwise, the Sensor's dictionary is updated with the
name and value.
Usage:
s = ownet.Sensor('/1F.5D0B01000000')
s.PIO_1 = '1'
will set the value of PIO.1 to 1.
"""
# print 'Sensor.__setattr__(%s, %s)' % (name, value)
# Life can get tricky when using __setattr__. Self doesn't
# have an _attrs atribute when it's initially created. _attrs
示例5: Request
# 需要导入模块: from connection import Connection [as 别名]
# 或者: from connection.Connection import read [as 别名]
class Request(object):
def __init__(self):
self.subprocess_env = []
self.uri = get_request_header("uri")
self.headers_in = {}
self.ap_auth_type = None
self.clength = 0
self.content_type = get_request_header("Content-Type")
self.path_info = self.uri
self.args = get_request_header("opts")
self.connection = Connection()
self.user = ""
self.method = get_request_header("cmd")
self.server = Server()
self.headers_out = {}
self.next = None
self.prev = None
self.main = None
self.the_request = self.method + " " + self.uri + " HTTP/1.1"
self.protocol = "HTTP"
self.__headers_sent = False
self.assbackwards = False
self.proxyreq = False
self.header_only = False
self.proto_num = 1001
self.hostname = ""
self.request_time = 0
self.status_line = "200 OK"
self.status = ""
self.method_number = 0
self.allowed = 0
self.allowed_xmethods = 0
self.allowed_methods = 0
self.sent_bodyct = 0
self.bytes_sent = 0
self.mtime = 0
self.chunked = False
self.range = ""
self.remaining = 0
self.read_length = 0
self.read_body = 1
self.read_chunked = False
self.expecting_100 = False
self.err_headers_out = {}
self.notes = None
self.phase = 0
self.interpreter = "python"
self.content_languages = []
self.handler = ""
self.content_encoding = ""
self.vlist_validator = 0
self.no_cache = True
self.no_local_copy = True
self.unparsed_uri = ""
self.filename = ""
self.canonical_filename = ""
self.finfo = None
self.parsed_uri = {}
self.used_path_info = ""
self.eos_sent = False
self.__cleanups = ()
self.__allowed = ()
def get_cleanups(self):
return self.__cleanups
def send_headers(self):
if len(self.__allowed) > 0:
set_response_header("Allowed", self.__allowed)
send_header()
def write(self, str, flush=False):
if not self.__headers_sent:
self.send_headers()
self.__headers_sent = True
ret = self.connection.write(str)
if flush:
self.flush()
return ret
def add_common_vars(self):
self.subprocess_env = []
def add_handler(self, type, handler, *dir):
pass
def add_input_filter(self, filter_name):
pass
def add_output_filter(self, filter_name):
pass
def allow_methods(self, methods, reset=False):
if reset:
self.__allowed = methods
else:
self.__allowed = self.__allowed + ", " + methods
#.........这里部分代码省略.........