我寫了一個簡單的http服務器和http客戶端代碼,我能夠成功地從客戶端發送消息到服務器的XML形式。這是我的代碼。扭曲,收到客戶端請求後返回客戶端自定義消息
client.py
from StringIO import StringIO
from twisted.internet import reactor
from twisted.web.client import Agent
from twisted.web.http_headers import Headers
from twisted.web.client import FileBodyProducer
from xml_dict import bm_xml
xml_str = bm_xml()
agent = Agent(reactor)
body = FileBodyProducer(StringIO(xml_str))
d = agent.request(
'GET',
'http://localhost:8080/',
Headers({'User-Agent': ['Replication'],
'Content-Type': ['text/x-greeting']}),
body)
def cbResponse(response):
print response.version
d.addCallback(cbResponse)
def cbShutdown(ignored):
reactor.stop()
d.addBoth(cbShutdown)
reactor.run()
server.py
from twisted.web import server, resource
from twisted.internet import reactor
def parse_xml(xml_str):
print xml_str
response = "xml_to_client"
return response
class Simple(resource.Resource):
isLeaf = True
def render_GET(self, request):
xml_request_str = request.content.read()
response = parse_xml(xml_request_str)
print response
site = server.Site(Simple())
reactor.listenTCP(8080, site)
reactor.run()
什麼,我想在這裏做的是從客戶端我發送一個XML字符串到服務器,從bm_xml模塊生成的XML這是另一個文件。這個XML被成功讀取到服務器,現在一旦服務器收到XML,我需要解析這個XML並返回另一個XML字符串。我有解析XML和構造另一個XML的代碼,以便客戶端從服務器接收這個XML,但我不知道如何將消息發送到客戶端從服務器。 在服務器或客戶端,所有需要更改的地方?我假設cbresponse
是必須在客戶端進行更改的人員,但我不知道應該在服務器端進行更改。 在服務器response
變量是我需要發送到客戶端。
你打算做的是同步還是異步連接?從上面的要求 – abhishekgarg
我想我想做異步連接。我不是隻發送簡單的xml結構的大量數據包。我不認爲同步是必要的。 – rakesh