Python interface Automation 14-multipart/form-data uploading images

Source: Internet
Author: User

Objective
When submitting the form operation, often encounter picture upload operation, picture upload is a separate interface, this article takes Zen Road as an example, describes how to upload pictures

First, upload interface

1. To submit a bug on Zen road, for example, when selecting a picture, click the OK button to upload the image.

2. Grab the packet with fiddler and look at the caught interface, the following interface is Multipart/form-data
-Content-type:multipart/form-data
-The body parameter is this format:

-----------------------------22165374713946
Content-disposition:form-data; Name= "Localurl"

Yoyoketang.png
-----------------------------22165374713946
Content-disposition:form-data; Name= "Imgfile"; Filename= "Yoyoketang.png"
Content-type:image/png


Second, login first

1. Because uploading pictures is done after login, this need to rely on user login

1 ```2 #Coding:utf-83 ImportRequests4 5Base ='http://127.0.0.1:81/'  #the server address of Zen Road6 7Loginurl = base+"/zentao/user-login.html"8 9h = {Ten     "user-agent":"mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) gecko/20100101 firefox/44.0", One     "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", A     "Accept-language":"zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3", -     "accept-encoding":"gzip, deflate", -     "Referer":"http://127.0.0.1/zentao/user-login.html", the     #"Cookie": # Do not use cookies until the head is logged in, as cookies are kept logged in -     "Connection":"keep-alive", -     "Content-type":"application/x-www-form-urlencoded", -     } +  -BODY = {" Account":"Admin", +         "Password":"e10adc3949ba59abbe56e057f20f883e", A         "keeplogin[]":" on", at         "Referer":"http://127.0.0.1/zentao/my/" -         } -  -s = requests.session ()#hold Session -R = S.post (loginurl, Data=body, headers=h) - PrintR.content#print results See location= ' http://127.0.0.1/zentao/my/' instructions login succeeded in```

Third, upload pictures

1. Upload the image in the following format:
-----------------------------22165374713946
Content-disposition:form-data; Name= "Localurl"

Yoyoketang.png
-----------------------------22165374713946
Content-disposition:form-data; Name= "Imgfile"; Filename= "Yoyoketang.png"
Content-type:image/png

2. Written in dictionary format, key corresponds to Name= "Imgfile" here name corresponding value

3.value inside is a tuple ()
-the first parameter is the corresponding value after filename=, no write None
-The second is the above value "" "Yoyoketang.png", or the binary stream opened by the file open
-The third is the content-type corresponding type, such as: "Image/png"

F ={
"Localurl": (None, "1.png"),
"Imgfile": ("1.png", Open ("D:\\1.png", "RB"), "Image/png")
}


Iv. Verifying the success of the upload
1. Get the post-upload address and put it in the browser's address bar request to see if the image you just uploaded


V. Reference code

1 #Coding:utf-82 ImportRequests3 4Base ='http://127.0.0.1:81/'  #the server address of Zen Road5 6Loginurl = base+"/zentao/user-login.html"7 8h = {9     "user-agent":"mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) gecko/20100101 firefox/44.0",Ten     "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", One     "Accept-language":"zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3", A     "accept-encoding":"gzip, deflate", -     "Referer":"http://127.0.0.1/zentao/user-login.html", -     #"Cookie": # Do not use cookies until the head is logged in, as cookies are kept logged in the     "Connection":"keep-alive", -     "Content-type":"application/x-www-form-urlencoded", -     } -  +BODY = {" Account":"Admin", -         "Password":"e10adc3949ba59abbe56e057f20f883e", +         "keeplogin[]":" on", A         "Referer":"http://127.0.0.1/zentao/my/" at         } -  -s = requests.session ()#hold Session -R = S.post (loginurl, Data=body, headers=h) - PrintR.content#print results See location= ' http://127.0.0.1/zentao/my/' instructions login succeeded -  in #Upload Image -URL1 ="Http://127.0.0.1:81/zentao/file-ajaxUpload-5a26aca290b59.html?dir=image" to  +f ={ -     "Localurl": (None,"1.png"), the     "Imgfile": ("1.png", Open ("D:\\1.png","RB"),"Image/png") *   } $R = S.post (URL1, files=f)Panax Notoginseng Try: -Jpgurl = Base+r.json () ["URL"] the     Print(U"URL address After uploading a picture:%s"%Jpgurl) + exceptException as msg: A     Print(U"return value is not in JSON format:%s"%str (msg)) the     Print(r.content) +```

Python interface Automation 14-multipart/form-data uploading images

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.