2011-12-14 55 views
5

我嘗試做簡單的異步HTTP客戶端asyncore: 此代碼工作正常,並輸出(快足夠多):asyncore蟒蛇掛

www.gmail.com : recv http code: 301 
www.yandex.ru : recv http code: 200 
www.python.org : recv http code: 200 
www.google.ru : recv http code: 200 
www.gravatar.com : recv http code: 302 
www.com.com : recv http code: 302 
www.yahoo.com : recv http code: 302 
www.bom.com : recv http code: 301 

但比我用不存在的主機取消註釋行:

#c = AsyncHTTP('http://www.no-such-host.ru') #!this line breaks execution! 

執行中斷,代碼掛起一段時間,數據的輸出部分,並沒有最後的數據輸出掛起:

connection error: [Errno -5] No address associated with hostname 
www.gmail.com : recv http code: 301 
www.yandex.ru : recv http code: 200 
www.yahoo.com : recv http code: 302 
www.com.com : recv http code: 302 
www.bom.com : recv http code: 301 
www.gravatar.com : recv http code: 302 

... 一些主機在這裏丟失,並在啓動時間延遲。

爲什麼會發生這種情況,以及如何解決這個問題?

# coding=utf-8 
import asyncore 
import string, socket 
import StringIO 
import mimetools, urlparse 

class AsyncHTTP(asyncore.dispatcher): 
    # HTTP requestor 

    def __init__(self, uri): 
     asyncore.dispatcher.__init__(self) 

     self.uri = uri 


     # turn the uri into a valid request 
     scheme, host, path, params, query, fragment = urlparse.urlparse(uri) 
     assert scheme == "http", "only supports HTTP requests" 
     try: 
      host, port = string.split(host, ":", 1) 
      port = int(port) 
     except (TypeError, ValueError): 
      port = 80 # default port 
     if not path: 
      path = "/" 
     if params: 
      path = path + ";" + params 
     if query: 
      path = path + "?" + query 

     self.request = "GET %s HTTP/1.0\r\nHost: %s\r\n\r\n" % (path, host) 

     self.host = host 
     self.port = port 

     self.status = None 
     self.header = None 
     self.http_code = None 
     self.data = "" 

     # get things going! 
     self.create_socket(socket.AF_INET, socket.SOCK_STREAM) 
     #self.connect((host, port)) 
     #return 
     try: 
      self.connect((host, port)) 
     except Exception,e: 
      self.close() 
      self.handle_connect_expt(e) 

    def handle_connect(self): 
     self.send(self.request) 

    def handle_expt(self): 
     print "handle_expt error!" 
     self.close() 

    def handle_error(self): 
     print "handle_error error!" 
     self.close() 

    def handle_connect_expt(self,expt): 
     print "connection error:",expt 

    def handle_code(self):   
     print self.host," : ","recv http code: ",self.http_code 


    def handle_read(self): 
     data = self.recv(2048) 
     #print data 
     if not self.header: 
      self.data = self.data + data 
      try: 
       i = string.index(self.data, "\r\n\r\n") 
      except ValueError: 
       return # continue 
      else: 
       # parse header 
       fp = StringIO.StringIO(self.data[:i+4]) 
       # status line is "HTTP/version status message" 
       status = fp.readline() 
       self.status = string.split(status, " ", 2) 
       self.http_code = self.status[1] 
       self.handle_code()  

       # followed by a rfc822-style message header 
       self.header = mimetools.Message(fp) 
       # followed by a newline, and the payload (if any) 
       data = self.data[i+4:] 
       self.data = "" 
       #header recived 
       #self.close() 


    def handle_close(self): 
     self.close() 




c = AsyncHTTP('http://www.python.org') 
c = AsyncHTTP('http://www.yandex.ru') 
c = AsyncHTTP('http://www.google.ru') 
c = AsyncHTTP('http://www.gmail.com') 
c = AsyncHTTP('http://www.gravatar.com') 
c = AsyncHTTP('http://www.yahoo.com') 
c = AsyncHTTP('http://www.com.com') 
c = AsyncHTTP('http://www.bom.com') 
#c = AsyncHTTP('http://www.no-such-host.ru') #!this line breaks execution! 
asyncore.loop() 

PS:我的系統的Ubuntu 11.10 +蟒蛇2.7.2

+0

在我的linux系統(不是ubuntu)上試過你的腳本,它運行時沒有任何你報告的問題。所以大概你的ubuntu安裝程序肯定會有一些問題。 – ekhumoro 2011-12-14 18:37:30

回答

4

當你這樣做self.connect((host, port))你調用一個阻塞名稱解析。結合本地DNS配置,這就是爲什麼你的程序在啓動時有很長的延遲。

作爲asyncore的另一種選擇,並找出如何自己完成非阻塞名稱解析,您可能會考慮使用Twisted。 Twisted的TCP連接設置API(主要是reactor.connectTCP或其上構建的其中一個API)不會被阻止。因此,它的一個天真的使用將保持正確的異步。