Python - Example of urllib2 asynchronous / threaded request using HTTPS

前端 未结 5 1370
梦谈多话
梦谈多话 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:30

    here is an example using urllib2 (with https) and threads. Each thread cycles through a list of URL's and retrieves the resource.

    import itertools
    import urllib2
    from threading import Thread
    
    
    THREADS = 2
    URLS = (
        'https://foo/bar',
        'https://foo/baz',
        )
    
    
    def main():
        for _ in range(THREADS):
            t = Agent(URLS)
            t.start()
    
    
    class Agent(Thread):
        def __init__(self, urls):
            Thread.__init__(self)
            self.urls = urls
    
        def run(self):
            urls = itertools.cycle(self.urls)
            while True:
                data = urllib2.urlopen(urls.next()).read()
    
    
    if __name__ == '__main__':
        main()
    

提交回复
热议问题