Use the Python httplib module to send a POST request to test if the Web service is working properly!

Source: Internet
Author: User

Recently in learning Python, just the boss recently let me do a POST request Test Web Service is normal to enable the small monitoring, the Internet to check the next information, found that the powerful Python can be used, so they are now learning to sell, by the way to exercise their own.

Because I also just contact this piece soon belongs to rookie level, so before the task, can only surf the web based on post request to understand clearly, these data online a lot, because I think the most important thing to accomplish this kind of task is to realize the principle of study clearly, write the program is just the tool to implement, if the big logic is incorrect , the back is all white busy.

After understanding how the post sends the request, the code for the logical construction using Python's Httplib module is as follows:

1 __author__='Tony_zhangl'2 #!/usr/bin/env python3 #Coding=utf84 ImportHttplib,urllib,json5HttpClient =None6WEBSUC, Webfal =0,07Infoofwebfalse = []8Listoffalseurl = []9Infoofserverfalse = []TenUrllist = [{'IP':"192.168.1.195",'Port': 8080,'Action':'XXX','Post':{'UID': 1,'VSN':'2.3','CType': 1}}, One{'IP':"192.168.1.195",'Port': 8080,'Action':'XXX','Post':{'UID': 3,'VSN':'2.3','CType': 1,'Activityareatype': 1,'page': 1,'Pagenum': 10}}, A{'IP':"192.168.1.195",'Port': 8080,'Action':'XXX','Post':{'UID': 3,'VSN': 2.3,'CType': 1}}, -{'IP':"192.168.1.195",'Port': 8080,'Action':'XXX','Post':{'UID': 3,'VSN':'2.3','CType': 1}}, -{'IP':"192.168.1.195",'Port': 8080,'Action':'XXX','Post':{'VSN':'2.3','CType': 1,'Channel': 1,'page': 1,'Pagenum': 10}}, the{'IP':"192.168.1.195",'Port': 8080,'Action':'XXX','Post':{'UID': 3,'VSN':'2.3','CType': 1,'page': 1,'Pagenum': 10}}] -  forItemsinchurllist: -     Try: -params = Urllib.urlencode (Items.get ('Post')) +headers = {"Content-type":"application/x-www-form-urlencoded","Accept":"Text/plain"} -HttpClient = Httplib. Httpconnection (Items.get ('IP'), Items.get ('Port'), timeout=30) +Httpclient.request ("POST", Items.get ('Action'), params,headers) AResponse =Httpclient.getresponse () at #Print Response.Status -Serverstatus =Response.reason - #print response.read () -TMP =Response.read () - #Print tmp -D =json.loads (TMP) in #print type (TMP) - #print type (d) to         ifServerstatus is  not 'OK': +             if  notD.get ('Success'): -Webfal + = 1 the Infoofwebfalse.append (d) * listoffalseurl.append (items) $             Else:Panax NotoginsengWebsuc +=1 -         Else: theWebfal +=1 + infoofserverfalse.append (Response.Status) A  the     exceptException, E: +         Printe -     finally: $         ifhttpClient: $ httpclient.close () - Print 'The number of success:' - PrintWebsuc the Print 'The number of false:' - PrintWebfalWuyi ifInfoofwebfalse is  notNone: the     Print 'The information of false for Webservice:' -     PrintInfoofwebfalse Wu ifInfoofserverfalse is  notNone: -     Print 'The information of false for Server:' About     PrintInfoofserverfalse $ ifListoffalseurl is  notNone: -     Print 'The list of URLs:' -     PrintListoffalseurl

I will test the URL and body are placed in the list of nested dictionaries, easy to follow the index processing.

The main problem encountered in the JSON format processing, because Response.read () is a line like a dictionary string, the first time in order to process the resulting field took a long while to process, because I am referring to Response.read () only the first time it is in effect , the later reference when found to be empty, because my page belongs to the small white level, can only help one of their buddies, in his analysis for a period of time, found that Response.read () is an object, can only be quoted once, if you want to continue to use, it must be assigned to their own defined variables, (PS: Maybe others use it relatively simple, but because of my unfamiliar, really do not understand the use of obj). In fact, the above I met is a small problem, although the overall idea, but the details of the implementation of their own treatment is relatively slow, should be I use not much and learning is not deep enough.

Because the first time in the blog Park blog, write a little messy, really sorry, for this task, I summarized as follows:

In fact, Urloflist best expressed as a tuple: because the content is immutable, as Python's original intention, the use of tuples is the most ideal method, but my skill is still poor, as long as the function will be realized, I will continue to improve in the late;

The final output also needs to go on at night, because the result is that it is not readable and needs to be perfected.

But as the first application, I deeply want to learn too much, but also need to continue to work, if there is a great God predecessors found that can improve a lot of, but also hope that many advice!!! Share!

Use the Python httplib module to send a POST request to test if the Web service is working properly!

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.