This article is participating in Python Theme Month. See the link to the event for more details

The era of universal programming, how can less test the figure of the students. Recently in learning Python, just live to learn live use, to combat a wave

Start ~

Background demand

One day, when I was not awake during my lunch break, I suddenly received @small pot from a Leader in the group, who uploaded a set of multimedia files to the hard disks of 100 devices on the platform. Online tasks needed to be tested.

– plan a

If I work hard and do not have many tasks, I may hum and hum, one by one equipment operation

  1. Remote login equipment
  2. Copy files from the local device to the device
  3. Repeat the previous two steps

I can’t. I have other tasks on hand, and the delivery date is today. It’s a waste of time

Following the traditional steps is repetitive and inefficient, so I need to find a developer to get the corresponding interface

– the second scheme

  1. Find the interface for the device to upload files
  2. Upload the file to the server and generate a download link
  3. Use Postman’s tools to request
  4. Repeat step 3 to change the device IP request

After trying it once or twice, it was too tedious to change the IP of the next device for each request, and the whole process didn’t save much time.

When I was confused, I was not learning Python. Let Python help me to do things, save the time to relax (^ – ^)V, thought of the third plan

Plan 3

  1. The relevant device function interface has been found
  2. Get the details of 100 devices from R&D (all devices are of the same model)
  3. Python script
  4. Run, rest

Okay, that’s more like it. No more talking. Let’s do it

The preparatory work

We need to prepare the environment

IDE: Pycharm version: Python3.7Copy the code

Python related libraries

HTTP library (request) download: PIP install Request josn download: used to parse URL related contentCopy the code

Premise:

  1. Analyze the interface request mode, request parameters, body, and return information (example)
Upload file interface: / API/Manager/dev/filetousb request: POST request parameters: the content-type = application/json Body: {" action ":" start ", "file: [{" fileurl" : "http://10.22.26.21/sw_bin/media/JueJing.jpg", "filename" : "JueJing. JPG", "directory":"", "filemd5":"12345678910999" } ] }Copy the code
  1. The files related to multimedia are all in the form of links. Therefore, the files are uploaded to the server in advance, and the file links and MD5 values are generated
  2. Simulate network requests to devices using the Python Request library
  3. The Body content is in the format of JOSN, so you need to use Python’s built-in JOSN library
  4. All 100 devices have different IP addresses, so if you find the list of device IP addresses in advance and put them in the text file, you need to use Python file management to read and put them in the list line by line, so that it is easy to pass values to request

hands-on

After the above preparation and analysis, also went to check the information, (too food, blind touch the river step by step to do)

Three next five in addition to two, a realization of debugging, write down the primary small white script (the big guys help to see, to teach)

Import requests the import interface using the json # usbfile = "/ API/Manager/dev/filetousb" # body content, Mediafile = {"action":"start", "file:[{"fileurl":"http://10.22.26.21/sw_bin/media/JueJing.jpg", "Filename ":" juejing.jpg ", "directory":"", "filemd5":"12345678910999"}]} Def do_post(IP, API,data): headers = {" content-type ": "application/json"} url = "http://"+ip+ ":8080" + api print (url) try: r = requests.post(url,headers = headers,data = json.dumps(data),timeout= 10) print(r.status_code) print(r.content.decode("utf-8")) except requests.exceptions.ConnectionError: Status_code = "Connection refused" with open("deviceip.txt","r") as f: while True: line = f.readline() if not line: break else: IP = [I for I in line] deviceiplist.append(IP) f.close() Print (iplist[I]) time.sleep(10) print(iplist[I]) time.sleep(10)Copy the code

When the process is running, we use the time we save to do other things and try to leave work early.

This time, more urgent, script writing is relatively simple, if you have better skills, welcome to leave a lot of messages, let me this small white increase vision.

conclusion

When learning Python, using Python scripts helps us to be more efficient and save time for other things

If you have better skills, let’s teach more ღ(´ · ᴗ · ‘)

Ok, so that’s a practice of using Python at work, welcome to comment and like, see you next time