A detailed web-side JSON communication protocol implemented by Python3

Source: Internet
Author: User
This article mainly introduces the Python3 implementation of the Web-side JSON communication protocol, with a certain reference value, interested in small partners can refer to.

Before the use of Python3 implemented TCP protocol, and then the implementation of the HTTP protocol communication, today the company wants to do a functional automated test system,

In the afternoon for a while, found that the implementation of the JSON format can be simpler, the code is as follows: Simple explanation, the general and server communication is divided into two pieces, a get protocol, a piece is the post protocol,

The Get protocol is very simple, directly accessible, the post protocol, in fact, the data used to use, the program will automatically identify the type.

3 questions were encountered during the writing process:

1 encountered an error while implementing the Post protocol,

Roughly speaking the problem of data format, the solution is very simple, to Utf-8 format: bytes (data, ' UTF8 '),

2 Get JSON data encountered encoding problem when encountering Chinese inside

The discovery shows that 0xaa0xbb0xcc0xdd such a code, JSON load call UTF8 can, use this code: Json.loads (Rawtext.decode (' UTF8 '))

3 Displays a long, long string of JSON when printed

Long string looked very painful, I can not see the JSON inside the relationship between the image, what the internet said with what Json.tool method to solve, but that is for the command line, I this is in the debugging process or want to print out directly,

Use the following code: print (Json.dumps (jsonstr, Sort_keys=false, ensure_ascii= False, indent=2)), it is important to note that the ensure_ascii must be False, Otherwise, there is Chinese in it.

You see 0xx or something, indent=2. Indicates that the JSON display is formatted, and Sort_keys indicates that the JSON does not need to be sorted


#!/usr/bin/evn python3#coding=utf-8# for the web-side JSON protocol communication, the communication protocol is JSON, the outgoing data is in JSON format, and the received information is also JSON format # External calls can initialize the Web_json class As follows: # get call # web = Web_json ("http://baidu.com/") # params = "abcd/select/100000?userid=1234&groupid=79" # Web.url_ Get (params) # # post Call # web = Web_json ("http://baidu.com/") # params = "abcd/select/100000" # data = ' {' name ': ' Jack ', ' ID ': "1"} ' # Web.url_post (params, data) from urllib.request import urlopenfrom urllib.parse import quoteimport jsonclass web_ Json:def __init__ (Self, base_url): Self.base_url = Base_url def get_url_data (self, params, data): web = Urlop En (Self.base_url + params, data) print (Web.url) print ("Status:", web.status) Rawtext = Web.read () jsonstr = Json.loads (Rawtext.decode (' UTF8 ')) print (Json.dumps (jsonstr, Sort_keys=false, ensure_ascii= False, indent=2)) r Eturn Jsonstr # Get Method def url_get (self, params): Return Self.get_url_data (params, None) # POST Method def Url_po St (self, params, data): Data=bytes (DatA, ' UTF8 ') return self.get_url_data (params, data) 


The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support topic.alibabacloud.com.

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.