但问题是:
如果不注释gtk.main()这一行,urllib2.urlopen()不返回。
只有关闭GUI窗口后,urllib2.urlopen()才返回。
此中原因我是在不明白,各位大虾帮忙
代码如下:
代码: 全选
import threading
import gtk
import urllib
import urllib2
class UrlThread(threading.Thread):
def __init__(self, url):
threading.Thread.__init__(self)
self.url = url
def run(self):
self.user_agent = 'Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11'
self.headers = { 'User-Agent' : self.user_agent }
request = urllib2.Request(self.url, headers=self.headers)
response = None
try:
response = urllib2.urlopen(request, timeout=10)
print response.read()
except:
pass
if __name__ == '__main__':
test = UrlThread('http://www.baidu.com/')
test.start()
window = gtk.Window()
window.add(gtk.Label('test'))
window.connect('destroy', gtk.main_quit)
window.show_all()
gtk.main()#关键是此行