Python - Example of urllib2 asynchronous / threaded request using HTTPS

前端 未结 5 1373
梦谈多话
梦谈多话 2021-02-02 13:50

I\'m having a heck of a time getting asynchronous / threaded HTTPS requests to work using Python\'s urllib2.

Does anyone out there have a basic example that implements u

5条回答
  •  情话喂你
    2021-02-02 14:32

    here is the code from eventlet

    urls = ["http://www.google.com/intl/en_ALL/images/logo.gif",
         "https://wiki.secondlife.com/w/images/secondlife.jpg",
         "http://us.i1.yimg.com/us.yimg.com/i/ww/beta/y3.gif"]
    
    import eventlet
    from eventlet.green import urllib2
    
    def fetch(url):
    
      return urllib2.urlopen(url).read()
    
    pool = eventlet.GreenPool()
    
    for body in pool.imap(fetch, urls):
      print "got body", len(body)
    

提交回复
热议问题