Module time, datetime, random, hashlib, requests

Keywords: Python Database SHA1

Catalog

package

What is a bag?

Packets are a form of modules, which are folders containing.py files.

Why bag?

At first, module has only a few functions. In the future, module expansion function, module name and usage should be better not to be modified, but this is convenient for users. For developers, module management is troublesome, so package is used to expand module functions.

1. Introduction of packages

  1. It's essentially a module. It's actually a folder with _init_ py files.
  2. The import package is to import the _init_.py file
  3. The package must be imported as a module file, and the search path of the module file depends on the path of the execution file.

2. Absolute and relative imports

Can only be used in packages

- Absolute import

from package name. Module name import method name

- Relative import

  • Represents the folder where the currently imported file is located
  • Represents the upper level of the folder currently being imported
  • ... represents the higher level of the folder currently being imported

time module

time stamp

import time

print(time.time())   # The number of seconds since 00:00 on January 1, 1970

Formatting time

import time 
print(time.strftime('%Y-%m-%d %H:%M:%S'))  # 2019-09-28 17:15:47

print(time.strftime('%Y-%m-%d %X'))   # 2019-09-28 17:16:50

Structured time

import time
print(time.localtime())   # time.struct_time(tm_year=2019, tm_mon=9, tm_mday=28, tm_hour=17, tm_min=18, tm_sec=11, tm_wday=5, tm_yday=271, tm_isdst=0)
# Structured base time
import time
print(time.localtime(0))   # time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=8, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)

sleep

import time 

start = time.time()
time.sleep(2)
end = time.time()
print(f'Suspended.{end - start}second')  # Pause 2.000108003616333 seconds

datetime module

import datetime

# Output current time
print(datetime.datetime.now())
# 2019-09-28 17:25:24.551237


# It requires extra times
now = datetime.datetime.now()
print(now + datetime.timedelta(days=3))
# 2019-10-01 17:28:24.710093


print(now.replace(year=1940))
# 1940-09-28 17:29:45.066855

random module

import random

# 0-1 random numbers
print(random.random())


# 1-100 Random Integers
print(random.randint(1,100))

# Random integers between 1 and 3
print(random.randrange(1, 3))

# Disturbing the order of LTS
lt = [1,2,4,60]
random.shuffle(lt)   # [4, 1, 60, 2]
print(lt)

# Random selection of an element in lt
print(random.choice(lt))

# random.seed
import random

random.seed(4)   # Give a random number seed
print(random.random())   # It's only the first random generation, and then the same number.
print(random.random())

# If you do not customize the seed, the seed will come at the current time

hashlib module

What is hash?

Hash is an algorithm (in version 3 of Python, hashlib module is used instead of MD5 module and sha module. It mainly provides SHA1, SHA224, SHA256, SHA384, SHA512, MD5 algorithm). The algorithm accepts the incoming content and obtains a series of hash values through calculation.

import hashlib

m = hashlib.md5()
m.update(b'sayhello')   # 981fe96ed23ad8b9554cfeea38cd334a
print(m.hexdigest())   # For different characters, never repeat

hash algorithm for database collision cracking

pwd_list = [
    'hash3714',
    'hash1313',
    'hash94139413',
    'hash123456',
    '123456hash',
    'h123ash',
]

hash_pwd = '0562b36c3c5a3925dbe3c4d32a4f2ba2'

for pwd in pwd_list:
    m = hashlib.md5()
    m.update(pwd.encode('utf-8'))
    res = m.hexdigest()
    if res in hash_pwd:
        print(f'Successful password acquisition:{pwd}')  # Successful password acquisition: hash123456

hmac module

Key Salting

import hmac

m = hmac.new(b'haha')
m.update(b'hash123456')
print(m.hexdigest())    # 24bb8daab11e526fc9b8178e51bc2ae7


m = m = hmac.new(b'sadness')
m.update(b'hash123456')
print(m.hexdigest())   # df405ffd019d6d3cd9a190fcab33aca5

requests module

It can be used to crawl data, simulate browser sending request to url, and get data.

import requests

response = requests.get('https://www.baidu.com')
print(response.text)

re module

Go to strings and find strings that match certain characteristics

Basic use of re module:

import re  # The first step is to introduce the re module
a = re.findall("Matching rule", "Does this string have a matching rule character?")  # The second step is to call the module function
print(a)  # Returns the matched string as a list

^ character

In order to... Start

s = 'abcdabc'

res = re.findall('^abc',s)  
print(res)                  # ['abc']
res = re.findall('^bc',s)
print(res)                  # []

$character

In order to... Ending

s = 'abcdabc'

res = re.findall('bc$',s)
print(res)             # ['bc']

...: Arbitrary characters

s = 'abc yes dabc'

res = re.findall('abc.',s)
print(res)   # ['abc is].

\d: Figures

s = 'asdhg213214h4c'

res = re.findall('\d',s)
print(res)   # ['2', '1', '3', '2', '1', '4', '4']

\ D: Non-digital

s = 'asdhg2132 -14h4c'

res = re.findall('\D',s)
print(res)   # ['a', 's', 'd', 'h', 'g', ' ', '-', 'h', 'c']

\ w: Non-empty, numbers, letters, underscores

s = 'asdhg213214h4c'

res = re.findall('\w',s)
print(res)   # ['a', 's', 'd', 'h', 'g', '2', '1', '3', '2', '1', '4', 'h', '4', 'c']

\ W: Empty, except for numbers, letters, underscores

s = 'as;g:21?32    -14h4c\n'

res = re.findall('\W',s)
print(res)  # [';', ':', '?', ' ', ' ', ' ', ' ', '-', '\n']

\s: empty

s = 'asdhg2132 14h4c'

res = re.findall('\s',s)
print(res)    # [' ']

\S: not empty.

s = 'asdhg2132    -14h4c\n'

res = re.findall('\S',s)
print(res)  # ['a', 's', 'd', 'h', 'g', '2', '1', '3', '2', '-', '1', '4', 'h', '4', 'c']

+ At least one character in front

s = 'abcdddd abcd abc ab'
print(re.findall('abc+', s))

?: The first character is 0-1.

s = 'abcdddd abcd abc ab a'
print(re.findall('abc?', s))  # ['abc', 'abc', 'abc', 'ab']

* At least 0 characters in the first one

s = 'abcdddd abcd abc ab a'
print(re.findall('abcd*', s))   # ['abcdddd', 'abcd', 'abc']
s = 'abc bbc cbc dbc'
print(re.findall('[abc]bc', s))  # ['abc', 'bbc', 'cbc']

[^]: Neither parentheses can be used.

s = 'abc bbc cbc dbc'
print(re.findall('[^abc]bc', s))  # ['dbc']

B: or

s = 'abc bbc dbc'
print(re.findall('abc|bbc', s))  # ['abc', 'bbc']

{2}: Two characters in front

s = 'abcccab abcc'
print(re.findall('abc{2}', s))   # ['abcc', 'abcc'] 
print(re.findall('abc{0,2}', s))  # ['abcc', 'ab', 'abcc']

Greedy mode

(Arbitrary characters)* (0-infinite)

s = 'abcdefgaaaaaaaaaaag'
print(re.findall('a.*g',s))
# ['abcdefgaaaaaaaaaaag']

Non-greedy model

(Arbitrary characters)* (0-infinite)?

s = 'abcdefgbbbbbbbg'
print(re.findall('a.*?g',s))   # ['abcdefg']

Understanding: Special Structures

# a(?=\d):a is followed by a number, but no number, no string content.
s = 'a123 aaaa a234 abc'
print(re.findall('a(?=\d)', s))  #['a', 'a']

print(re.findall('a(?=\w)', s))  #['a', 'a', 'a', 'a', 'a', 'a']

compile

# In the early days, re.findall could not pass mode, but could only use compile.
s = '#@#@#@nickchen121@163.com$$$$////nick@qq.com$$#$#$[]]2287273393@162.com@$2423423lksdlfj#'
email_pattern = re.compile('\w+@\w+.com')
phone_patter = re.compile('\d{13}')
print(re.findall(email_pattern, s))

match: Find one from the beginning, and you can't find it, you can't find it, and you can't make a mistake.

s = 'abcd abcddd abc'
res = re.match('abcd*', s)
print(res.group())  # abcd

search: Find one from the string, and you won't find it.

s = 'abcd abcddd abc'
res = re.search('abcd*', s)
print(res.group())  # abcd

split segmentation

s = 'adad213114242wjdnadjia1241423daj'
print(re.split('\d+', s))  # ['adad', 'wjdnadjia', 'daj']

sub replacement

s = 'adad213114242wjdnadjia1241423daj'
print(re.sub('\d+', '  ', s))  # adad  wjdnadjia  daj

subn replacement, how many times more than sub replacement

s = 'adad213114242wjdnadjia1241423daj'
print(re.subn('\d+', '  ', s))   # ('adad  wjdnadjia  daj', 2)

Supplement: re.S

s = '''abc
abcabc*abc
'''

print(re.findall('abc.abc',s ))  # ['abc*abc'] Original. Mismatched line feeds

print(re.findall('abc.abc',s ,re.S))  # ['abc\nabc', 'abc*abc']

Grouping: as long as it's in parentheses

s = 'abc abcd abcddd'
print(re.findall('a(.)c(d)', s))  # [('b', 'd'), ('b', 'd')]

Named grouping

s = 'abc abcd abcddd'
print(re.search('a(?P<name1>.)c(?P<name2>d)', s).groupdict()) # {'name1': 'b', 'name2': 'd'}

Ultra-advanced usage

s = 'abc123abc123'  # c123a
print(re.sub('c(\d+)a', ' ', s))
print(re.sub('c(?P<name1>\d+)a', ' \g<name1> ', s))  # \ g < name1 > This thing can't be replaced

# ab bc123
# ab 123 bc123

Posted by Shendemiar on Mon, 07 Oct 2019 00:55:07 -0700