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()
サーバー.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 文字列をサーバーに送信することです。xml は別のファイルである bm_xml モジュールから生成されます。この xml はサーバーに正常に読み込まれます。サーバーが xml を受信したら、この xml を解析して別の xml 文字列を返す必要があります。クライアントがサーバーからこのxmlを受信できるように、xmlを解析して別のxmlを構築するコードがありますが、サーバーからクライアントにメッセージを送信する方法がわかりません。サーバーまたはクライアントで、すべての変更が必要な場所はどこですか? クライアントで変更を行う必要があるのは 1 つであると想定しcbresponse
ていますが、サーバー側で変更を行う必要がある場所がわかりません。サーバーresponse
変数には、クライアントに送信する必要がある変数があります。