Post/get package construction in Python and generation of random strings

Source: Internet
Author: User
Now, let's use Python to create a get package and a post package.

As to what is useful, we slowly realize.

Python contains a large number of libraries, and as a new language, Python has strong enough support for HTTP.

Now, we're introducing the new library Httplib and Urllib

These two libraries are based on the name, and we can know that they are for HTTP as well as URL operations.

First we need to establish a connection with the server. (We use a micro-blog as an example to achieve the various functions below)

conn = Httplib. Httpconnection ("ti50*****com");

As long as there is no prompt error, we can assume that the connection is successful, and the following can send the packet.

In the above we have mentioned the structure of the get package, only the hearder part. In Httplib, Heaer is saved by a dictionary. Let's define it here:

headers = {"Content-type": "application/x-www-form-urlencoded",

"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",

"Referer": "http://ti50.*****com/g/s?sid=*********************",

"Accept-encoding": "",

"Accept-language": "zh-cn,zh;q=0.8",

"Accept-charset": "gbk,utf-8;q=0.7,*;q=0.3",

"Cookie": Cookie}

Accept-encoding we delete its content, this for the server, our client can not accept any compressed format, the packet will be sent back with the original data, so we can save the process of decompression analysis of the Web page, but the consequences of this is large traffic, network real-time poor. There are other libraries that specialize in extracting natural information about decompression.

Then we can send it directly.

Conn.request (method= "GET", url= "http://ti50****com/g/s?*********_tk9eh&r=" + Go_num + "&aid=amsg& Bid=******=true&ifh=1&ngpd=false ", headers=headers);

The method field describes what type of packet is sent.

The URL field defines the address in the form of a string

Header field defines header.

Typically, a packet is sent to the server, and the server returns an answer packet accordingly. And this response packet is often useful for us, and we use the following command to get the reply packet.

Response = Conn.getresponse ();

For parentheses in the above statement, it represents the number of characters before the answer packet is read.

The post package is basically the same as the Get package creation process.

It's just that we need a new definition body, which can be defined in the form of a string.

params = ' msg=*************************** '

We still need to connect before the server.

conn = Httplib. Httpconnection ("ti50*****com");

Send

Conn.request (method= "POST", Url= '/g/s?sid=******************&ngpd=false ", body=params,headers=headers);

It can be found that the above formula is slightly different from the format of the send get package.

Method has changed.

There is no write domain in the URL.

One more body field.

The second one can be thought of, if the domain name is not defined, the system will be the most recent connection with the server to replace the domain name.

The answer package is obtained in the same way as the get package.

A mess of small applications.

(i) generation of random strings.

When we use post to do something very interesting, we often encounter the server to verify the god horse, sometimes we can use random strings to deal with such situations.

Python gives a library of random numbers ... random.

Very convenient for simple applications. For example, we produce a random integer between A and B.

Random.randint (A, B)

>>> Random.randint (10,20)

>>> 15

Knowing this step, we can easily write a random string of the program,

From random import randomdef random_str (randomlength):    str = '    chars = ' AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789 '    length = len (chars)-1    random = random () For    i in range (randomlength):        str+=chars[random.randint (0, length)]    return str

It is obvious that a random string length should be given when calling this function.

Of course, we can also define the characters in the random string by modifying the character in the chars.

(ii) Program run time

We now give a very imprecise method of calculating the time of the program,

From time import clock as Nowstart = Now () finish = Now () Run_time = Finish-startprint Run_time
  • 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.