我在扭曲的服务器上使用sendline()做错了什么?

时间:2012-03-15 13:19:18

标签: python twisted

我正在尝试一次将文件的内容发送到客户端1行 这样客户端(用Objective-C编写)就可以单独处理每一行。 但是,客户端的日志显示正在从服务器发送的数据 所有这一切都是以一条线的形式出现,而且显然太大,所以它会切断中间线 通过一行导致客户端因意外语法而崩溃的方法。

我在服务器上做了什么(用扭曲的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()

非常令人沮丧,我确信这很简单。感谢。

1 个答案:

答案 0 :(得分:0)

这个问题涉及经常提出的主题。有关同一问题的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客户端,作为你不当行为的来源。