Python-timed dynamic acquisition of IP proxy pool, stored in file

Source: Internet
Author: User

Timing function is implemented via module time + dead loop because Time.sleep () automatically blocks

get_ip_pool.py
"""    @__note__: while True + time.sleep实现定时更新代理池文件(ip_pool)    @__how_to_use: 1. 直接另开终端运行。(但终端会阻塞) 2. 后台运行。 Unix, 后台运行脚本: <script_name> &"""import requestsimport timePROXIES_URL = ‘‘def get_ip_pool(url=PROXIES_URL):    """ 根据URL构造代理池    :param PROXIES_URL:    :return: []    """    response = requests.get(url)    with open(‘./ip_pool‘, ‘w‘) as f:        f.write(response.text)        f.flush()    # time+死循环, 实现定时功能。    time.sleep(10)    print(‘get_ip_pool is done‘)while True:    get_ip_pool()
read_ip_pool.py
import randomdef read_ip_pool():    with open(‘IP_POOL‘, ‘r‘) as f:        ip_list = f.readlines()        ip = random.choice(ip_list).strip()        # print(‘random ip is: ‘ + ip)    print(‘read ip_pool is done‘)# read_ip_pool()

Python-timed dynamic acquisition of IP proxy pool, stored in file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.