source: python/stackless/udp_server.py @ 43

Revision 43, 767 bytes checked in by sgk, 11 years ago (diff)

見た目

  • Property svn:executable set to *
Line 
1#!/usr/bin/env python
2#vim:fileencoding=utf-8
3
4import stackless
5import sys, stacklesssocket as socket
6sys.modules["socket"] = socket
7
8def udp_server(socket_address, handler):
9  sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
10  sock.bind(socket_address)
11  while True:
12    (data, address) = sock.recvfrom(8192)
13    stackless.tasklet(handler)(sock, data, address)
14
15def udp_handler(sock, data, address):
16  import time
17  print '%.6f %r %r' % (time.time(), data, address)
18  sock.sendto(data, address)    # エコーバック
19  #XXX 時間がかかる処理を行う場合は、適宜の次の行をはさむ。
20  # stackless.schedule()
21
22def test():
23  stackless.tasklet(udp_server)(('0.0.0.0', 2000), udp_handler)
24  stackless.run()
25
26if __name__ == '__main__':
27  test()
Note: See TracBrowser for help on using the repository browser.