2013-06-06 186 views
0

我寫了一個簡單的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變量是我需要發送到客戶端。

+0

你打算做的是同步還是異步連接?從上面的要求 – abhishekgarg

+0

我想我想做異步連接。我不是隻發送簡單的xml結構的大量數據包。我不認爲同步是必要的。 – rakesh

回答

2

你的代碼似乎已經做了你所要求的。 Simple.render_GET返回responseresponse是發送給客戶端的內容。也許你不確定如何在發送給客戶端後得到響應?如果是這樣,答案可能是readBodymore docs)。

+0

是的,這是真的,我發送到客戶端後無法獲得響應。我嘗試了'打印響應'。它向我展示了物體的位置。它必須是一行代碼對嗎?我在這裏錯過了一些東西。 – rakesh

+0

嗨@ Jean-Paul-Calderone,感謝您的鏈接,但對我來說這並沒有太大的進步,如果問題不大,能否發佈答案,請給我很多幫助。 – rakesh

+0

你讀過鏈接去的頁面嗎?他們給你答案。我不打算在這裏爲你重寫文檔。 –

0

我找到了我想要的東西:這是我更新的代碼爲client.py

from StringIO import StringIO 

from twisted.internet import reactor 
from twisted.internet.protocol import Protocol 
from twisted.web.client import Agent 
from twisted.web.http_headers import Headers 
from twisted.internet.defer import Deferred 

from twisted.web.client import FileBodyProducer 
from xml_dict import bm_xml 

xml_str = bm_xml() 
agent = Agent(reactor) 
body = FileBodyProducer(StringIO(xml_str)) 

class BeginningPrinter(Protocol): 
    def __init__(self, finished): 
     self.finished = finished 
     self.remaining = 1024 * 10 

    def dataReceived(self, bytes): 
     if self.remaining: 
      reply = bytes[:self.remaining] 
      print reply 

    def connectionLost(self, reason): 
     print 'Finished receiving body:', reason.getErrorMessage() 
     self.finished.callback(None) 


    d = agent.request(
    'POST', 
    'http://localhost:8080/', 
    Headers({'User-Agent': ['Replication'], 
      'Content-Type': ['text/x-greeting']}), 
    body) 

    def cbRequest(response): 
     finished = Deferred() 
     response.deliverBody(BeginningPrinter(finished)) 
     return finished 

    d.addCallback(cbRequest) 

    def cbShutdown(ignored): 
    reactor.stop() 

    d.addBoth(cbShutdown) 
    reactor.run() 

,這是我server.py

from twisted.web import server, resource 
    from twisted.internet import reactor 
    from xml_parser import parser 
    from twisted.web.resource import Resource 

    def parse_xml(xml_str): 
     print xml_str 
     xml_response = parser(xml_str) 
     return xml_response 

class re_simple(Resource): 
     isLeaf = True 
     def render_POST(self, request): 
      xml_request_str = request.content.read() 
      xml_response = parse_xml(xml_request_str) 
      return xml_response 

site = server.Site(re_simple()) 
reactor.listenTCP(8080, site) 
print "server started" 
reactor.run() 

現在我將請求發送:<some Xml>服務器 我從服務器<some response>得到迴應。

這就是我想要的。