文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>利用SocketServer实现新协议

利用SocketServer实现新协议

时间:2008-01-02  来源:hkcat2006

可以用SocketServer模块实现自己的协议,下面这个例子返回给客户端不同格式的时间

#!/usr/bin/env python
from SocketServer import ThreadingMixIn, TCPServer, StreamRequestHandler
import time

class TimeRequestHandler(StreamRequestHandler):
    def handle(self):
        req = self.rfile.readline().strip()
        if req == "asctime":
            result = time.asctime()
        elif req == "seconds":
            result = str(int(time.time()))
        elif req == "rfc822":
            result = time.strftime("%a, %d %b %Y %H:%M:%S +0000",
                     time.gmtime())
        else:
            result = """Unhandled request. Send a line with one of the
following words:

asctime -- for human-readable time
seconds -- seconds since the Unix Epoch
rfc822 -- date/time in format used for mail and news posts"""
        self.wfile.write(result + "\n")

class TimeServer(ThreadingMixIn, TCPServer):
    allow_reuse_address = 1

serveraddr = ('', 8765)
srvr = TimeServer(serveraddr, TimeRequestHandler)
srvr.serve_forever()

相关阅读 更多 +
排行榜 更多 +
找茬脑洞的世界安卓版

找茬脑洞的世界安卓版

休闲益智 下载
滑板英雄跑酷2手游

滑板英雄跑酷2手游

休闲益智 下载
披萨对对看下载

披萨对对看下载

休闲益智 下载