本文整理匯總了Python中asyncore.loop方法的典型用法代碼示例。如果您正苦於以下問題:Python asyncore.loop方法的具體用法?Python asyncore.loop怎麽用?Python asyncore.loop使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類asyncore
的用法示例。
在下文中一共展示了asyncore.loop方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: line_terminator_check
# 需要導入模塊: import asyncore [as 別名]
# 或者: from asyncore import loop [as 別名]
def line_terminator_check(self, term, server_chunk):
event = threading.Event()
s = echo_server(event)
s.chunk_size = server_chunk
s.start()
event.wait()
event.clear()
time.sleep(0.01) # Give server time to start accepting.
c = echo_client(term, s.port)
c.push("hello ")
c.push("world%s" % term)
c.push("I'm not dead yet!%s" % term)
c.push(SERVER_QUIT)
asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
s.join()
self.assertEqual(c.contents, ["hello world", "I'm not dead yet!"])
# the line terminator tests below check receiving variously-sized
# chunks back from the server in order to exercise all branches of
# async_chat.handle_read
示例2: test_close_when_done
# 需要導入模塊: import asyncore [as 別名]
# 或者: from asyncore import loop [as 別名]
def test_close_when_done(self):
s, event = start_echo_server()
s.start_resend_event = threading.Event()
c = echo_client('\n', s.port)
c.push("hello world\nI'm not dead yet!\n")
c.push(SERVER_QUIT)
c.close_when_done()
asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
# Only allow the server to start echoing data back to the client after
# the client has closed its connection. This prevents a race condition
# where the server echoes all of its data before we can check that it
# got any down below.
s.start_resend_event.set()
s.join()
self.assertEqual(c.contents, [])
# the server might have been able to send a byte or two back, but this
# at least checks that it received something and didn't just fail
# (which could still result in the client not having received anything)
self.assertTrue(len(s.buffer) > 0)
示例3: Pinging_Thread
# 需要導入模塊: import asyncore [as 別名]
# 或者: from asyncore import loop [as 別名]
def Pinging_Thread(self):
print "[+] Starting Ping thread"
wait = True
while 1: #loop forever
if wait: sleep(ping_delay) #send ping to server interval
self.mutex_http_req.acquire() #Ensure that the other thread is not making a request at this time
try:
resp_data=HTTPreq(url,"") #Read response
if verbose: v_print(pings_n=1)
if resp_data: #If response had data write them to socket
if verbose: v_print(received_d_pt=len(resp_data))
self.send(resp_data) #write to socket
resp_data="" #clear data
#not clearing flag in case more data avail.
wait = False #Dont wait: if there was data probably there are more
else:
wait = True
finally:
self.mutex_http_req.release()
print "[-] Pinging Thread Exited"
thread.interrupt_main() #Signal main thread -> exits
示例4: interact
# 需要導入模塊: import asyncore [as 別名]
# 或者: from asyncore import loop [as 別名]
def interact(self, stdin=sys.stdin, prompt='i> ', verbose=False):
"Starts an interactive command loop reading commands from the consolle"
try:
import readline
readline_present = True
except ImportError:
readline_present = False
if stdin is sys.stdin and readline_present: # use readline
histfile = os.path.expanduser('~/.%s.history' % self.name)
completions = list(self.commands) + list(self.mpcommands) + \
list(self.thcommands) + list(self.tm.specialcommands)
self.stdin = ReadlineInput(completions, histfile=histfile)
else:
self.stdin = stdin
self.prompt = prompt
self.verbose = verbose
intro = self.obj.__doc__ or ''
write(intro + '\n')
with self:
self.obj._interact_ = True
if self.stdin is sys.stdin: # do not close stdin automatically
self._manage_input()
else:
with self.stdin: # close stdin automatically
self._manage_input()
示例5: test_quick_connect
# 需要導入模塊: import asyncore [as 別名]
# 或者: from asyncore import loop [as 別名]
def test_quick_connect(self):
# see: http://bugs.python.org/issue10340
if self.family in (socket.AF_INET, getattr(socket, "AF_INET6", object())):
server = BaseServer(self.family, self.addr)
t = threading.Thread(target=lambda: asyncore.loop(timeout=0.1,
count=500))
t.start()
def cleanup():
t.join(timeout=TIMEOUT)
if t.is_alive():
self.fail("join() timed out")
self.addCleanup(cleanup)
s = socket.socket(self.family, socket.SOCK_STREAM)
s.settimeout(.2)
s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER,
struct.pack('ii', 1, 0))
try:
s.connect(server.address)
except OSError:
pass
finally:
s.close()
示例6: serve_forever
# 需要導入模塊: import asyncore [as 別名]
# 或者: from asyncore import loop [as 別名]
def serve_forever(self, poll_interval):
"""
Run the :mod:`asyncore` loop until normal termination
conditions arise.
:param poll_interval: The interval, in seconds, used in the underlying
:func:`select` or :func:`poll` call by
:func:`asyncore.loop`.
"""
try:
asyncore.loop(poll_interval, map=self._map)
except OSError:
# On FreeBSD 8, closing the server repeatably
# raises this error. We swallow it if the
# server has been closed.
if self.connected or self.accepting:
raise
示例7: line_terminator_check
# 需要導入模塊: import asyncore [as 別名]
# 或者: from asyncore import loop [as 別名]
def line_terminator_check(self, term, server_chunk):
event = threading.Event()
s = echo_server(event)
s.chunk_size = server_chunk
s.start()
event.wait()
event.clear()
time.sleep(0.01) # Give server time to start accepting.
c = echo_client(term, s.port)
c.push(b"hello ")
c.push(b"world" + term)
c.push(b"I'm not dead yet!" + term)
c.push(SERVER_QUIT)
asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
s.join(timeout=TIMEOUT)
if s.is_alive():
self.fail("join() timed out")
self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"])
# the line terminator tests below check receiving variously-sized
# chunks back from the server in order to exercise all branches of
# async_chat.handle_read
示例8: start
# 需要導入模塊: import asyncore [as 別名]
# 或者: from asyncore import loop [as 別名]
def start(type):
sys.stdout.flush()
info["type"] = type
s.send((json.dumps(info)+ "\n").encode('utf-8'));
sys.stdout = Printer()
sys.stderr = Printer()
asyncore.loop()
示例9: run
# 需要導入模塊: import asyncore [as 別名]
# 或者: from asyncore import loop [as 別名]
def run():
foo = EmlServer(('0.0.0.0', 25), None)
try:
asyncore.loop()
except KeyboardInterrupt:
pass
示例10: serve_forever
# 需要導入模塊: import asyncore [as 別名]
# 或者: from asyncore import loop [as 別名]
def serve_forever(self):
'Block and begin accepting connections.'
try:
asyncore.loop(5, map = self)
except StopNode, e:
pass
示例11: begin_loop
# 需要導入模塊: import asyncore [as 別名]
# 或者: from asyncore import loop [as 別名]
def begin_loop(self):
'Sub-classes can override this to handle the start of the event loop.'
pass
示例12: items
# 需要導入模塊: import asyncore [as 別名]
# 或者: from asyncore import loop [as 別名]
def items(self):
# map.items() gets called at the top of the asyncore loop
self.begin_loop()
# how long since we called heartbeat?
now = time.time()
if now - self._last_heartbeat > 10:
self._last_heartbeat = now;
self.heartbeat()
return self._peers.items()
示例13: serve_forever
# 需要導入模塊: import asyncore [as 別名]
# 或者: from asyncore import loop [as 別名]
def serve_forever(self):
"""
Process requests until :py:meth:`BaseSMTPServer.shutdown` is called.
"""
asyncore.loop()
示例14: run
# 需要導入模塊: import asyncore [as 別名]
# 或者: from asyncore import loop [as 別名]
def run(self):
self.active = True
self.__flag.set()
while self.active and asyncore.socket_map:
self.active_lock.acquire()
asyncore.loop(timeout=0.1, count=1)
self.active_lock.release()
asyncore.close_all(ignore_all=True)
示例15: run
# 需要導入模塊: import asyncore [as 別名]
# 或者: from asyncore import loop [as 別名]
def run(self):
self.active = True
if self.flag:
self.flag.set()
while self.active:
try:
asyncore.loop(1)
except:
pass