• The bytes/str dichotomy in Python 3


    The bytes/str dichotomy in Python 3 - Eli Bendersky's website https://eli.thegreenplace.net/2012/01/30/the-bytesstr-dichotomy-in-python-3

     Arguably the most significant new feature of Python 3 is a much cleaner separation between text and binary data. Text is always Unicode and is represented by the str type, and binary data is represented by thebytes type. What makes the separation particularly clean is that str and bytes can't be mixed in Python 3 in any implicit way. You can't concatenate them, look for one inside another, and generally pass one to a function that expects the other. This is a good thing.

    import requests
    import re
    import time
    from redis import Redis

    REDIS_HOST, REDIS_PORT, PASSWORD = '192.168.2.51', '6379', 'mypwd'
    rds = Redis(host=REDIS_HOST, port=REDIS_PORT, password=PASSWORD)

    f, url_l, filter_replace_l = 'DISTINCT_url.txt', [], [' ', ' ', ' ']
    with open(f, 'r', encoding='utf-8') as fr:
    for i in fr:
    try:
    for ii in filter_replace_l:
    i = i.replace(ii, '')
    rds.sadd('chk_url_all', i)
    except Exception as e:
    print(e)

    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'}
    while True:
    try:
    url = rds.spop('chk_url_all').decode('utf-8')
    s = 'http://'
    if s not in url:
    url = '{}{}'.format(s, url)
    print(url)
    r = requests.get(url, headers=headers, timeout=50)
    print(r.status_code)
    except Exception as e:
    print(e)



    从redis取值结果为bytes type



  • 相关阅读:
    Sqli-labs less 12
    Sqli-labs less 13
    Sqli-labs less 14
    Python3之collections模块
    Python3之 contextlib
    Python3之sqlalchemy
    Python3之RabbitMQ
    Python3之redis使用
    Python3之Memcache使用
    python自动化开发学习 进程, 线程, 协程
  • 原文地址:https://www.cnblogs.com/rsapaper/p/8478011.html
Copyright © 2020-2023  润新知