2020-03-12 08:24:38 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
import struct
|
|
|
|
import random
|
2021-03-01 10:40:07 +00:00
|
|
|
import socket
|
|
|
|
import string
|
|
|
|
|
|
|
|
|
2021-06-29 04:54:25 +00:00
|
|
|
string_punctuation = '!#$%&()*+,-.:;<=>?@[]^_~'
|
2020-03-12 08:24:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def random_datetime(date_start, date_end):
|
|
|
|
random_delta = (date_end - date_start) * random.random()
|
|
|
|
return date_start + random_delta
|
|
|
|
|
|
|
|
|
|
|
|
def random_ip():
|
|
|
|
return socket.inet_ntoa(struct.pack('>I', random.randint(1, 0xffffffff)))
|
|
|
|
|
|
|
|
|
2021-03-01 10:40:07 +00:00
|
|
|
def random_string(length, lower=True, upper=True, digit=True, special_char=False):
|
|
|
|
chars = string.ascii_letters
|
|
|
|
if digit:
|
|
|
|
chars += string.digits
|
|
|
|
|
|
|
|
while True:
|
|
|
|
password = list(random.choice(chars) for i in range(length))
|
|
|
|
if upper and not any(c.upper() for c in password):
|
|
|
|
continue
|
|
|
|
if lower and not any(c.lower() for c in password):
|
|
|
|
continue
|
|
|
|
if digit and not any(c.isdigit() for c in password):
|
|
|
|
continue
|
|
|
|
break
|
|
|
|
|
|
|
|
if special_char:
|
|
|
|
spc = random.choice(string_punctuation)
|
|
|
|
i = random.choice(range(len(password)))
|
|
|
|
password[i] = spc
|
|
|
|
|
|
|
|
password = ''.join(password)
|
|
|
|
return password
|