Python-implemented JSON data is requested in an HTTP get,post,put,delete-mode page

Source: Internet
Author: User

I. INTRODUCTION of JSON

JSON (JavaScript Object Notation) is a lightweight data interchange format. Easy for people to read and write. It is also easy for machine parsing and generation.
It is based on JavaScript programming Language, Standard ECMA-262 a subset of 3rd Edition-december 1999.
JSON takes a completely language-independent text format, but also uses a similar idiom to the C language family (c, C + +, C #, Java, JavaScript, Perl, Python, etc.).
These features make JSON an ideal data exchange language.

Second, the HTTP request method

The http/1.1 protocol defines eight methods (sometimes called "actions") to indicate the different modes of operation of the Request-uri specified resources:
. Options-Returns the HTTP request method that the server supports for a specific resource.
You can also test the functionality of your server with a request to send a ' * ' to the Web server.
. HEAD-asks the server for a response that is consistent with the GET request, except that the response body will not be returned.
This method allows you to obtain meta information contained in the response message header without having to transmit the entire response content.
. GET-Makes a request to a specific resource.
Note: The Get method should not be used in operations that produce "side effects", such as in Web apps.
One of the reasons is that get can be accessed by web spiders and other casual.
. POST-submits data to the specified resource for processing requests (such as submitting a form or uploading a file).
The data is included in the request body. A POST request may result in the creation of new resources and/or modification of existing resources.
. PUT-uploads its latest content to the specified resource location.
. Delete-the request server deletes the resource identified by the Request-uri.
. TRACE-echo the request received by the server, primarily for testing or diagnostics.
. The connect-http/1.1 protocol is reserved for proxy servers that can change connections to pipelines.
. PATCH-Used to apply local modifications to a resource, added to the canonical RFC5789.

Among them, Get,post, PUT, delete is commonly used in the implementation of RESTful APIs, so the following code implementation

Third, the python implementation of JSON data in HTTP Get,post,put,delete way page request

Gossip less, directly on the code.

1. Get method

#!/usr/bin/env python
#-*-Coding:utf-8-*-
# File:http_get.py

Import Urllib2

Def http_get ():
Url= ' Http://192.168.1.13:9999/test ' #页面的地址
Response = Urllib2.urlopen (URL) #调用urllib2向服务器发送get请求
Return Response.read () #获取服务器返回的页面信息

ret = Http_get ()
Print ("ret%r"% (ret))

2. Post method

#!/usr/bin/env python
#-*-Coding:utf-8-*-
# File http_post.py

Import Urllib
Import Urllib2
Import JSON

Def http_post ():
Url= ' Http://192.168.1.13:9999/test '
Values ={' user ': ' Smith ', ' passwd ': ' 123456}

Jdata = Json.dumps (values) # JSON format encoding of data
req = Urllib2. Request (URL, jdata) # Generate full data for page requests
Response = Urllib2.urlopen (req) # Send page request
Return Response.read () # Gets the page information returned by the server

RESP = Http_post ()
Print RESP

3. Put method

#!/usr/bin/env python
#-*-Coding:utf-8-*-
# File:http_put.py

Import Urllib2
Import JSON

Def http_put ():
Url= ' Http://192.168.1.13:9999/test '
values={': '}

Jdata = Json.dumps (values) # JSON format encoding of data
Request = Urllib2. Request (URL, jdata)
Request.add_header (' Content-type ', ' Your/conntenttype ')
Request.get_method = lambda: ' PUT ' # Sets the way HTTP is accessed
Request = Urllib2.urlopen (Request)
Return Request.read ()

RESP = Http_put ()
Print RESP

4. Delete method

#!/usr/bin/env python
#-*-Coding:utf-8-*-
# File:http_delete.py

Import Urllib2
Import JSON

Def http_delete ():
Url= ' Http://192.168.1.13:9999/test '
values={' user ': ' Smith '}

Jdata = Json.dumps (values)
Request = Urllib2. Request (URL, jdata)
Request.add_header (' Content-type ', ' Your/conntenttype ')
Request.get_method = lambda: ' DELETE ' # Sets the way HTTP is accessed
Request = Urllib2.urlopen (Request)
Return Request.read ()

RESP = Http_delete ()
Print RESP

Python-implemented JSON data is requested in an HTTP get,post,put,delete-mode page

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.