2013-07-31 70 views
1

Im編碼連接到遠程服務器的python腳本,並分析返回的響應。出於某種奇怪的原因,10次中的9次,一旦讀取頭部,腳本就會繼續並在返回響應正文之前返回。我沒有在Python的專家,但即時通訊肯定我的代碼是正確的東西的Python的一面。這裏是我的代碼:部分讀取來自Socket.File.Read

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次,read不返回任何內容,或者僅返回部分數據。

使用例:

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])) 
+0

什麼,準確地說,是你的問題? –

+0

我的問題是爲什麼有時我得到一個完整的閱讀和其他時間我不 – Wilson212

回答

2

不正確的答案刪除,但尚未刪除的(這樣的評論可以住了一段時間。)

+0

在http.read()後移動http.Close()後,我仍然得到部分讀取 – Wilson212

+0

我無法重現您看到的行爲,使用www .google.com /,stackoverflow.net/,或linux.die.net/。你可以測試一個公共可見的Web服務器,並告訴我一個它違反的Web服務器的名稱? –

+0

我其實只是發現錯誤,但我仍然不知道如何解決它。例外(10054,'通過對等方重置連接')。即時通訊測試我的本地HttpListener(.Net 4.0) – Wilson212