python - 从 Socket.File.Read 部分读取

标签 python sockets

我正在编写一个连接到远程服务器的 python 脚本,并解析返回的响应。由于某些奇怪的原因,十有八九,一旦读取了 header ,脚本就会继续并在获取响应正文之前返回。我不是 python 专家,但我确信我的代码在 python 方面是正确的。这是我的代码:

class miniclient:
"Client support class for simple Internet protocols."

def __init__(self, host, port):
    "Connect to an Internet server."


    self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    self.sock.settimeout(30)

    try:
        self.sock.connect((host, port))
        self.file = self.sock.makefile("rb")

    except socket.error, e:

        #if e[0]    == 111:
        #   print   "Connection refused by server %s on port %d" % (host,port)
        raise


def writeline(self, line):
    "Send a line to the server."

    try:
        # Updated to sendall to resolve partial data transfer errors
        self.sock.sendall(line + CRLF) # unbuffered write

    except socket.error, e:
        if e[0] == 32 : #broken pipe
            self.sock.close() # mutual close
            self.sock = None

        raise e

    except socket.timeout:
        self.sock.close() # mutual close
        self.sock = None
        raise

def readline(self):
    "Read a line from the server.  Strip trailing CR and/or LF."

    s = self.file.readline()

    if not s:
        raise EOFError

    if s[-2:] == CRLF:
        s = s[:-2]

    elif s[-1:] in CRLF:
        s = s[:-1]

    return s


def read(self, maxbytes = None):
    "Read data from server."

    if maxbytes is None:
        return self.file.read()

    else:
        return self.file.read(maxbytes)


def shutdown(self):

    if self.sock:
        self.sock.shutdown(1)


def close(self):

    if self.sock:
        self.sock.close()
        self.sock = None

我使用 ReadLine() 方法读取标题,直到到达空行(标题和正文之间的分隔符)。从那里,我的对象只需调用“Read()”方法来读取正文。如前所述,读取 10 次中有 9 次不会返回任何内容,或者只返回部分数据。

使用示例:

try:
    http = miniclient(host, port)

except Exception, e:

    if e[0] == 111:
        print   "Connection refused by server %s on port %d" % (host,port)

    raise

http.writeline("GET %s HTTP/1.1" % str(document))
http.writeline("Host: %s" % host)
http.writeline("Connection: close") # do not keep-alive
http.writeline("")
http.shutdown() # be nice, tell the http server we're done sending the request

# Determine Status
statusCode = 0
status = string.split(http.readline())
if status[0] != "HTTP/1.1":
    print "MiniClient: Unknown status response (%s)" % str(status[0])

try:
    statusCode = string.atoi(status[1])
except ValueError:
    print "MiniClient: Non-numeric status code (%s)" % str(status[1])

#Extract Headers
headers = []
while 1:
    line = http.readline()
    if not line:
        break
    headers.append(line)

http.close() # all done

#Check we got a valid HTTP response
if statusCode == 200:
    return http.read()
else:
    return "E\nH\terr\nD\tHTTP Error %s \"%s\"\n$\tERR\t$" % (str(statusCode), str(status[2]))

最佳答案

在调用 http.read() 之前先调用 http.close()。延迟对 http.close() 的调用,直到读取完所有数据之后。

关于python - 从 Socket.File.Read 部分读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17961625/

相关文章:

java - C 服务器到 Java 客户端套接字读取以短值传递

javascript - 通过 Route 在 React 元素之间共享数据

python - 切片 DataFrameGroupBy 对象

c - epoll接收返回值

Java IOException Stream 在服务器客户端程序中关闭

PHP 使用 Fsockopen 发布数据

python - 访问词典项目列表

python - PyCharm:列出一个类的所有方法的所有用法

java - 如何在不破坏应用程序输出的情况下重定向 JVM 输出?

windows - 无法在 Eclipse 中运行 junit 测试