在远程使用所有数据之前,Python套接字已关闭

时间:2015-06-03 12:52:29

标签: python sockets

我正在编写一个Python模块,它通过unix套接字与go程序进行通信。客户端(python模块)将数据写入套接字,服务器使用它们。

    SELECT DISTINCT
        `annonce`.`idAnnonce`, `annonce`.`reference` 
    FROM
        `annonce`, `freeDays` 
    WHERE
        `annonce`.`idAnnonce` = `freeDays`.`idAnnonce`
    AND
        `freeDays`.`date` = '2015-06-06'
    AND
        `freeDays`.`date` = '2015-06-07'

我的问题是,在服务器有效读取数据之前,Python客户端倾向于完成并关闭套接字,从而导致服务器端的“管道损坏,连接重置”。无论我做什么,对于Python代码,一切都已发送,因此对send()sendall()select()的调用都是成功的......

提前致谢

编辑:我因mac OS而无法使用关机

EDIT2:我也尝试删除超时并调用setblocking(True),但它不会改变任何内容

EDIT3:准备好这个问题http://bugs.python.org/issue6774后,文档似乎不必要了,所以我恢复了关机,但我仍然遇到同样的问题:

# Simplified version of the code used
outputStream = socket.socket(socketfamily, sockettype, protocol)
outputStream.connect(socketaddress)
outputStream.setblocking(True)
outputStream.sendall(message)
....
outputStream.close()

2 个答案:

答案 0 :(得分:0)

IHMO最好使用Asynchornous I / O库/框架。这是使用circuits

的解决方案

服务器回显它接收到stdout的内容,客户端打开文件并将其发送到服务器,等待它完成,然后关闭套接字并终止。这是通过Async I / O和Coroutines的混合完成的。

<强> server.py:

from circuits import Component
from circuits.net.sockets import UNIXServer

class Server(Component):

    def init(self, path):
        UNIXServer(path).register(self)

    def read(self, sock, data):
        print(data)

Server("/tmp/server.sock").run()

<强> client.py:

import sys

from circuits import Component, Event
from circuits.net.sockets import UNIXClient
from circuits.net.events import connect, close, write

class done(Event):
    """done Event"""

class sendfile(Event):
    """sendfile Event"""

class Client(Component):

    def init(self, path, filename, bufsize=8192):
        self.path = path
        self.filename = filename
        self.bufsize = bufsize

        UNIXClient().register(self)

    def ready(self, *args):
        self.fire(connect(self.path))

    def connected(self, *args):
        self.fire(sendfile(self.filename, bufsize=self.bufsize))

    def done(self):
        raise SystemExit(0)

    def sendfile(self, filename, bufsize=8192):
        with open(filename, "r") as f:
            while True:
                try:
                    yield self.call(write(f.read(bufsize)))
                except EOFError:
                    break
                finally:
                    self.fire(close())
                    self.fire(done())

Client(*sys.argv[1:]).run()
  

在我对它的测试中,它的行为完全符合我的预期   错误和服务器在客户端clsoes之前获取完整的文件   插座并关闭。

答案 1 :(得分:0)

在与一位知道C套接字的同事讨论之后(在cpython中,套接字模块是C套接字的包装器),他谈到了这个http://ia600609.us.archive.org/22/items/TheUltimateSo_lingerPageOrWhyIsMyTcpNotReliable/the-ultimate-so_linger-page-or-why-is-my-tcp-not-reliable.html(这就是如何在PHP内部为记录完成的)

TL&amp; DR:在Linux上关机+快速投票+关闭或ioctl(SIOCOUTQ)