This article mainly introduces the web-side json communication protocol implemented by python3, which has some reference value. if you are interested, you can refer to it. This article mainly introduces the web-side json communication protocol implemented by python3, which has some reference value. if you are interested, you can refer to it.
Previously, python3 was used to implement tcp and later http communication. Today, the company wants to develop an automatic function testing system,
After a while in the afternoon, we found that the implementation of the json format can be simpler. the code is as follows: For a brief explanation, communication with the server is generally divided into two parts: one is the get protocol and the other is The post protocol,
The get protocol is simple and can be accessed directly. The post protocol actually uses data and the program will automatically identify the type.
Three problems encountered during writing:
1. an error occurred while implementing the post protocol,
To address the problem of data format, the solution is simple. convert to UTF-8 format: bytes (data, 'utf8 '),
2. encoding problems occur when the obtained json data contains Chinese characters.
The code 0xaa0xbb0xcc0xdd is displayed. you can call utf8 when loading json. use this code: json. loads (rawtext. decode ('utf8 '))
3. a long and long string is displayed when json is printed.
The long string is very painful, and the relationship between various pairs in json is not clear at all. what json is used on the Internet. tool solution, but it is for the command line. I still want to print it out during the debugging process,
Use the following code: print (json. dumps (jsonStr, sort_keys = False, ensure_ascii = False, indent = 2). Note that ensure_ascii must be False. otherwise, when there is a Chinese character in it
If you see 0xx or something, indent = 2 indicates formatting json display, and sort_keys indicates that this json does not need to be sorted.
#! /Usr/bin/evn python3 # coding = UTF-8 # for the communication library of the web-side json protocol, the communication protocol is json and the outgoing data is in json format, the received data is also in json format # The web_json class can be initialized for external calls, as shown below: # 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 = urlopen (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) return jsonStr # get method def url_get (self, params): return self. get_url_data (params, None) # post method def url_post (self, params, data): data = bytes (data, 'utf8') return self. get_url_data (params, data)
The above is all the content of this article. I hope it will help you learn and support PHP.
For more details about the web-side json communication protocol implemented by python3, please follow the PHP Chinese network!