python模組學習string之造測試資料

來源:互聯網
上載者:User

標籤:struct   規則   ring   phone   body   mask   unp   code   random   

測試過程中需要一些隨機資料,使用到了python中的string模組,記錄一下

#! /usr/bin/python# coding:utf-8 """ @author:Bingo.he @file: data_factory.py @time: 2017/12/21 """import socketimport structimport stringimport randomdef random_phoneNo():    """擷取隨機號碼"""    return ‘1‘ + ‘‘.join(random.choice(string.digits) for i in range(10))def random_upper_code(length):    """擷取大寫字母與數位隨機碼"""    return ‘‘.join(random.choice(string.ascii_uppercase + string.digits) for i in range(length))def random_lower_code(length):    """擷取小寫字母與數位隨機碼"""    return ‘‘.join(random.choice(string.ascii_lowercase + string.digits) for i in range(length))def random_IP():    """擷取隨機IP"""    RANDOM_IP_POOL = [‘202.69.19.222/0‘]    str_ip = RANDOM_IP_POOL[random.randint(0, len(RANDOM_IP_POOL) - 1)]    str_ip_addr = str_ip.split(‘/‘)[0]    str_ip_mask = str_ip.split(‘/‘)[1]    ip_addr = struct.unpack(‘>I‘, socket.inet_aton(str_ip_addr))[0]    mask = 0x0    for i in range(31, 31 - int(str_ip_mask), -1):        mask = mask | (1 << i)    ip_addr_min = ip_addr & (mask & 0xffffffff)    ip_addr_max = ip_addr | (~mask & 0xffffffff)    return socket.inet_ntoa(struct.pack(‘>I‘, random.randint(ip_addr_min, ip_addr_max)))

PS: 博主公司使用的手機號校正規則就是1開頭,11位元字即可通過校正

 

python模組學習string之造測試資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.