Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This paper has participated inProject DigginTo win the creative gift package and challenge the creative incentive money

Multithreaded development may encounter problems

Assuming that two threads, T1 and T2, are incrementing the global variable g_num(default: 0) by 1, t1 and T2 each incremented g_num 10 times. The final result of g_num should be 20.

However, due to the simultaneous operation of multiple threads, the following situations may occur:

When g_num=0, t1 returns g_num=0. The system assigns t1 to “sleeping” and T2 to “running”. T2 also gets g_num=0 and then t2 adds 1 to g_num so that g_num=1 and then t2 is scheduled to “sleeping”. Change t1 to “running”. Thread T1 adds one to the 0 it gained before and assigns g_num. This results in g_num=1, even though both T1 and T2 add 1

Test 1

import threading import time g_num = 0 def work1(num): global g_num for i in range(num): g_num += 1 print("----in work1, g_num is %d---"%g_num) def work2(num): global g_num for i in range(num): g_num += 1 print("----in work2, G_num is % d - "% g_num) print (" -- -- -- before Thread creation g_num is % d -" % g_num) t1 = threading. Thread (target = work1, args=(100,)) t1.start() t2 = threading.Thread(target=work2, args=(100,)) t2.start() while len(threading.enumerate()) ! = 1: time. Sleep (1) print(" %s" % g_num)Copy the code

Running results:

-- Before thread creation g_num is 0--- ----in work1, g_num is 100--- ----in work2, g_num is 200-- After two threads operate on the same global variable, the result is :200Copy the code

To enter the body

Target website :tlvod.com/v-57381.htm… (Fast and Furious 9)

Note: The article has accompanying video tutorial, concern private me

Tool use

Development tools: pycharm development environment: python3.7, Windows10 using third-party libraries: requests

Dynamic packet capture after playing to see what the data is. After a closer look, I found that the videos are all fragments of TS files

When I saw TS, I suddenly realized that this is the legendary M3U8 video format

Let’s refresh the page to find the M3U8 ending file

How do you know he’s the one who’s wondering? Simply copy this and visit it

I created a new download task when I accessed it, but I didn’t have the suffix

Download and play the problem is a small snippet

Below use regular expressions to extract them (note: worry about reading the article zero-based regular expressions, white and white write simple ugly point good understanding really can not go to the regular expression website first learn about it.)

Import requests # import re # regular expressions from TQDM import TQDM # proxy browser: 'Mozilla/5.0 '(Windows NT 10.0; Win64; X64) AppleWebKit / 537.36 (KHTML, Like Gecko) Chrome/93.0.4577.63 Safari/ 537.36edg /93.0.961.38'} Response = requests. Get (url, Headers =headers) return response def save(URL, name): "" Store the TS fragment file :param URL: TS address :param name: TS name: return: F = open('./video/{}.ts'. Format (name), 'ab') # hexadecimal file to read and write a file additional b f.w rite (response) f. lose () url = 'https://c1.monidai.com/20210907/SOPKxzCy/index.m3u8' response Text response = re.sub(r'#EXTM3U', ", response) # replace response = re.sub(r'#EXTM3U', ", Response) # replace d integer response = re.sub(r'# ext-x-targetDuration: d*', ', Response) # replace response = re.sub(r'# ext-x-media-sequence :\d*', ", response) # replace response = re.sub(r'#EXTINF:\d.\d*,', Response = re.sub(r'# ext-x-endList ', ", response) response = re.sub(r'#EXTINF:\d\d, ", ", response) response = re.sub(r'#EXTINF:\d,', '', response) ts_url = response.split() for link in tqdm(ts_url): Name = link.split('/')[-1] # save(link, nameCopy the code

These TS files need to be merged after the video is downloaded

And I’m going to do it but this code is going to improve if you want to try multithreading.

I am white and white I, a program yuan like to share knowledge ❤️ interested can follow my public account: White and white learning Python [very thank you for your likes, booklets, comments, three links support]