Python requests implements automatic testing of image upload interface

Recently, I wrote a small request for others. I need local automatic screenshots, and then automatically upload the pictures to the cloud. It is very simple to realize automatic screenshots. I will not introduce them in detail here. I will mainly write down how to upload locally through Python+requests. The picture is taken to the cloud server again.

Not much to say, because we want to use the requests library to automate the image upload interface, the first step is to follow the requests.

1. Installation:

pip install requests

2. Open code:

1. Because we need to upload pictures, we have to log in first and take a picture of the cloud to get cookies. The code for implementing the login interface is as follows:

def getCookies(self,username,password):
url = "https://console.upyun.com/accounts/signin/"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json, text/plain, */*',
'Referer': 'https://console.upyun.com/login/',
'Accept - Encoding': 'gzip, deflate, br',
'Accept - Language': 'zh - CN, zh;q = 0.9'
}
body = {
'password': password,
'username': username
}
requests.packages.urllib3.disable_warnings()
r = requests.post(url, data=body, headers=headers, verify=False)
cookies = r.cookies

return cookies

2. At this point, we can call the login interface to get cookies, so that we can directly upload pictures through the upload picture interface.

cookies = getCookies("zhangsan","123qwe")

headersUpload = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
'Accept': 'application/json, text/plain, */*',
'Referer': 'https://console.upyun.com/services/adver/filemanage/',
'Accept - Encoding': 'gzip, deflate, br',
'x-file-size': '209482',
'Content-Length': '209663',
'Accept - Language': 'zh - CN, zh;q = 0.9'
}

files = {"file": (filename, open(picPath+filename, "rb"), "image/png")}
requests.packages.urllib3.disable_warnings()
try:
r = requests.put(
"https://console.upyun.com/folder/projectA/test//"+filename, files=files, headers=headersUpload,
cookies=cookies, verify=False)
except BaseException as e:
print("上传图片失败!",str(e))

Finally, I would like to thank everyone who has read my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, you can take it away if you need it:

insert image description here

Software testing interview applet

The software test question bank maxed out by millions of people! ! ! Who is who knows! ! ! The most comprehensive quiz mini program on the whole network, you can use your mobile phone to do the quizzes, on the subway or on the bus, roll it up!

The following interview question sections are covered:

1. Basic theory of software testing, 2. web, app, interface function testing, 3. network, 4. database, 5. linux

6. web, app, interface automation, 7. performance testing, 8. programming basics, 9. hr interview questions, 10. open test questions, 11. security testing, 12. computer basics

These materials should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can help you too!   

Guess you like

Origin blog.csdn.net/qq_48811377/article/details/132475118