這是一個使用扭曲框架的快速「概念驗證」應用程序。這假設傳統的UDP服務正在監聽localhost:8000,並將開始發送UDP數據以響應包含「發送數據」的數據報。數據是3 32位整數。此外,將一個 「HTTP GET /」 端口響應2080
您可以用twistd -noy example.py
開始這樣的:
example.py
from twisted.internet import protocol, defer
from twisted.application import service
from twisted.python import log
from twisted.web import resource, server as webserver
import struct
class legacyProtocol(protocol.DatagramProtocol):
def startProtocol(self):
self.transport.connect(self.service.legacyHost,self.service.legacyPort)
self.sendMessage("Send me data")
def stopProtocol(self):
# Assume the transport is closed, do any tidying that you need to.
return
def datagramReceived(self,datagram,addr):
# Inspect the datagram payload, do sanity checking.
try:
val1, val2, val3 = struct.unpack("!iii",datagram)
except struct.error, err:
# Problem unpacking data log and ignore
log.err()
return
self.service.update_data(val1,val2,val3)
def sendMessage(self,message):
self.transport.write(message)
class legacyValues(resource.Resource):
def __init__(self,service):
resource.Resource.__init__(self)
self.service=service
self.putChild("",self)
def render_GET(self,request):
data = "\n".join(["<li>%s</li>" % x for x in self.service.get_data()])
return """<html><head><title>Legacy Data</title>
<body><h1>Data</h1><ul>
%s
</ul></body></html>""" % (data,)
class protocolGatewayService(service.Service):
def __init__(self,legacyHost,legacyPort):
self.legacyHost = legacyHost #
self.legacyPort = legacyPort
self.udpListeningPort = None
self.httpListeningPort = None
self.lproto = None
self.reactor = None
self.data = [1,2,3]
def startService(self):
# called by application handling
if not self.reactor:
from twisted.internet import reactor
self.reactor = reactor
self.reactor.callWhenRunning(self.startStuff)
def stopService(self):
# called by application handling
defers = []
if self.udpListeningPort:
defers.append(defer.maybeDeferred(self.udpListeningPort.loseConnection))
if self.httpListeningPort:
defers.append(defer.maybeDeferred(self.httpListeningPort.stopListening))
return defer.DeferredList(defers)
def startStuff(self):
# UDP legacy stuff
proto = legacyProtocol()
proto.service = self
self.udpListeningPort = self.reactor.listenUDP(0,proto)
# Website
factory = webserver.Site(legacyValues(self))
self.httpListeningPort = self.reactor.listenTCP(2080,factory)
def update_data(self,*args):
self.data[:] = args
def get_data(self):
return self.data
application = service.Application('LegacyGateway')
services = service.IServiceCollection(application)
s = protocolGatewayService('127.0.0.1',8000)
s.setServiceParent(services)
有感
這不是一個WSGI設計。這個想法可以用來運行這個守護程序,並使其在本地IP和Apache上的http端口或類似的代理請求。它可以重構爲WSGI。這種方式更快,更容易調試。
HTTP中沒有「連續的」。根據定義。不可能有。你在問什麼? HTTP協議的奇蹟版本,它不會遵循HTTP規則,而是像UDP一樣行事? – 2010-09-22 10:15:26
謝謝你的關心。我知道這樣的事情不存在。澄清:我的問題是試圖通過HTTP讀取UDP數據的快照。我並沒有試圖讓HTTP「連續」(問題也被編輯過) – jsalonen 2010-09-22 10:47:54
一個好的結構可以使某種webapp中的某些變量模型以某種方式實現。 UDP數據更新那個模型(即,webapp監聽它),但不告訴任何HTTP要更新的東西。 HTTP的任何提取只是從這些變量中讀取當前狀態的快照(並記住禁用所生成頁面的緩存)。 – 2010-09-22 11:23:18