获取临时IP加入临时列表使用

1. 根据相应提示生成API 链接,随后

def get_ip_lin():
    # 创建临时IP储存表
    res_ls = []
    url = "http://xxxxxxxxxxxxxx"
    response = requests.get(url)
    if response.status_code == 200:
        for res in response.json():
            res_ls.append(res.get("ip") + ":" + res.get("port"))
        print("请求代理,", res_ls)
        return res_ls

    return []


def func1(ip_list):
    if len(ip_list) > 0:
        ip = ip_list.pop()
    else:
        while True:
            ip_list.extend(get_ip_lin())
            if not ip_list:
                time.sleep(10)
            else:
                break
        ip = ip_list.pop()
    print("ip", ip)
    return ip
code

 

2. PyMySQL链接数据库获取值

import pymysql
def get_ip():
    db = pymysql.connect(
        host='ip',
        user='用户名',
        password='密码',
        port=3306,
        db='数据库名称'
    )
    cursor = db.cursor()
    sql = '''SELECT id,ip FROM 表名称 where status = 0 ORDER BY id desc limit 10'''
    cursor.execute(sql)
    res = cursor.fetchall()
    if res:
        ids = str([res__[0] for res__ in res]).replace("[", "(").replace("]", ")")
        cursor.execute('''update 表名 set status = 1  where id in{}'''.format(ids))
        db.commit()

        return [{"https": res_[1].replace("http", "https")} for res_ in res]
    return None 
Code

 

posted @ 2020-12-06 17:32  Mr-刘  阅读(104)  评论(0编辑  收藏  举报