Python urllib2 http GET, POST request differences

Source: Internet
Author: User

Python urllib2 implementing a Get POST request for HTTPpython can generally use the module URLLIB2 to implement the Get post HTTP requestGET Request
    1. Import Os,sys

    2. Import Os,sys

    3. Import Urllib2

    4. Http_str = ' http://127.0.0.1:12345/apps/' + serviceline + '/clusters/' + clustername

    5. url = http_str + '/machine_info '

    6. req = Urllib2. Request (URL) # URL converted to URL to initiate GET request

    7. result = Urllib2.urlopen (req) # initiates get HTTP service

    8. res = Result.read () #把结果通过. Read () function reads

    9. Slave_info = json.loads (res) #把返回结果 (string) is converted to JSON for ease of processing,

    10. Print slave_info[' content ' [' slave '] # printing dict information for JSON strings


post request "POST and get to go to request the difference is that some of the parameters are written inside the program, not in the HTTP request"

    1. Import Os,sys

    2. Import Os,sys

    3. Import Urllib2

    4. Http_str = ' http://127.0.0.1:12345/apps/' + serviceline + '/clusters/' + clustername

    5. URL1 = http_str + '/ops/rpm_update ' # URL part

    6. Params1 = {"app_rpm_id": "1", "rpm_xxx": rpmxxxxx} #参数部分

    7. The params = Json.dumps (params1) # HTTP server requires the incoming params to be a JSON object, so you need to convert it here, and if the HTTP server is not required, you can not convert

    8. Response = Urllib.urlopen (url1,params) #发送POST request Print Response # because the HTTP server has already specified that the result of the return is a JSON object, so there is no need to convert to JSON;

    9. Print Response.read () # reads the result data through read (), and if HTTP is not returning a JSON object, it needs to be converted to a JSON object.


Python urllib2 http GET, POST request differences

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.