簡體   English   中英

我在扭曲的服務器中使用sendline()怎么辦?

[英]What am I doing wrong with sendline() in my twisted server?

我正在嘗試一次將文件內容發送到客戶端1行,以便客戶端(用Objective-C編寫)可以分別處理每一行。 但是,客戶端的日志顯示,從服務器發送過來的數據全部以1行的形式傳入,並且顯然太大,因此它從一行的中途中斷,導致客戶端由於意外的語法而崩潰。

我在服務器上做的事情(用python編寫,帶有扭曲)導致線不單獨發送嗎?

這是目前阻止我使用的服務器中的特定代碼。

def sendLine(self, line): 
    self.transport.write(line + '\r\n') 

def updateShiftList(self):

    #open the datesRequested file for the appropriate store and load the dates into a list
    fob = open('stores/'+self.storeName+'/requests/datesRequested','r')
    DATES_REQUESTED = fob.read()
    datesRequested = DATES_REQUESTED.split('\n')
    #open each date file that is listed in datesRequested
    for date in datesRequested:
        if os.path.isfile('stores/'+self.storeName+'/requests/' + date):
            fob2 = open('stores/'+self.storeName+'/requests/' + date,'r')
            #load the file into memory and split the individual requests up
            THE_REQUESTS = fob2.read()
            thedaysRequests = THE_REQUESTS.split('\n')
            for oneRequest in thedaysRequests:
                if len(oneRequest) > 4:
                    print "*)[*_-b4.New_REQUEST:"+oneRequest
                    self.sendLine('*)[*_-b4.New_REQUEST:'+oneRequest)
            fob2.close()
    fob.close()

非常令人沮喪,我相信這很容易。 謝謝。

這個問題涉及一個經常提出的話題。 關於同一問題,stackoverflow上存在許多問題:

等等

TCP發送有序的,可靠的數據

  • 排序意味着先發送的字節先到達。
  • 可靠性意味着將發送發送的字節,否則連接將斷開。 數據不會被靜默刪除。

流式傳輸是您最關心的問題。 流不會分成單獨的消息。 它由字節組成,這些字節之間的“邊界”可以任意移動。

如果您發送“ hello”,然后發送“ world”,則流中這兩個字符串之間的邊界可能會消失。 對等方可能會收到“ hello,world”或“ h”,“ ello,world”或“ he”,“ ll”,“ o”,“ w”,“ or”或“ ld”。

這就是人們使用面向行協議的原因。 每個邏輯消息末尾的“ \\ r \\ n”允許接收方緩沖並將流拆分成那些原始邏輯消息。

要進行更深入的探討,我推薦以下有關PyCon演示的視頻: http : //pyvideo.org/speaker/417/glyph

所有這些都指向連接的另一端,即ObjC客戶端,這是行為不端的根源。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM