Python constructs an HTTP request

Source: Internet
Author: User

We often use python to capture packets, simulate login, and so on. It is necessary to construct an HTTP request packet.

HTTP requests usually have four methods: Get, post, put, and delete, which correspond to query, update, add, and delete. We often use get and post. 1. Construct get # Build request for accessed URL in Python
Homereq = urllib. Request. Request (
Url = csdnaccessmoduleurl
)

homereq. add_header (& apos; accept & apos;, & apos; text/html, application/XHTML + XML, */* & apos;);
homereq. add_header (& apos; accept-language & apos;, & apos; en-US & apos;)
homereq. add_header (& apos; accept-Encoding & apos;, & apos; gzip, deflate & apos;)
homereq. add_header (& apos; connection & apos;, & apos; keep-alive & apos;);
homereq. add_header (& apos; Referer & apos;, & apos; http://passport.csdn.net/account/l Ogin? From = & apos; + urllib. parse. quote (moduleurl) # HTTP % 3A % 2f % 2fwrite.blog.csdn.net % 2f & apos;)
homereq. add_header (& apos; User-Agent & apos;, & apos; Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; wow64; Trident/5.0) & apos ;);

# Open Access URL
Resp = urllib. Request. urlopen (homereq) 2. Construct post # Build request for login URL using Python
# Post Data
Postdata = {
& Apos; U & apos;: username,
& Amp; apos; P & amp; apos;: password,
& Apos; remember & apos;: & apos; 1 & apos ;,
& Apos; T & apos;: & apos; log & apos ;,
& Apos; F & apos;: urllib. parse. Quote (moduleurl ),
}

Postdata = urllib. parse. urlencode (postdata). encode (& apos; UTF-8 & apos ;)
Print (postdata)

Req = urllib. Request. Request (
Url = csdnloginurl,
Data = postdata)

Req. add_header (& apos; accept & apos;, & apos; text/html, application/XHTML + XML, */* & apos ;);
Req. add_header (& apos; accept-language & apos;, & apos; en-US & apos ;)
Req. add_header (& apos; accept-Encoding & apos;, & apos; gzip, deflate & apos ;)
Req. add_header (& apos; connection & apos;, & apos; keep-alive & apos ;);
Req. add_header (& apos; Referer & apos;, csdnaccessmoduleurl)
Req. add_header (& apos; User-Agent & apos;, & apos; Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; wow64; Trident/5.0) & apos ;);

# Open login URL
r = urllib. request. urlopen (req) through the above two examples, we can find that when the request postdata value is assigned, it is the POST method, the default is get.

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.