この記事では主に Python3 で実装された Web 側の JSON 通信プロトコルを紹介します。興味のある方は参考にしてください。
以前は python3 を使用して tcp プロトコルを実装していましたが、その後 http プロトコル通信を実装しました。今日、会社は機能的な自動テスト システムを作成したいと考えています。 json 形式のコードは次のとおりです。 簡単に説明すると、サーバーとの通信は通常 2 つの部分に分かれており、1 つは get プロトコルで、もう 1 つは post プロトコルです。 post プロトコルは実際にデータ データを使用し、プログラムはそのデータに直接アクセスし、タイプを自動的に識別します。
書き込みプロセス中に 3 つの問題が発生しました:
1 post プロトコルの実装時にエラーが発生しました
大まかに言えば、問題は utf-8 形式に変換することです。 (data , 'utf8'),
2 取得した json データに中国語が含まれていると、エンコーディングの問題が発生しました。json をロードするときに utf8 を呼び出すだけです。 loads( rawtext.decode('utf8'))
3 json を出力すると、非常に長い文字列が表示されます
長い文字列は読むのが非常に苦痛で、json 内のオブジェクト間の関係を明確に見ることはできません。インターネットでは、問題を解決するにはどのような json.tool メソッドを使用する必要があると言われていますが、それはコマンドライン用であり、デバッグプロセス中に直接出力したいと考えています
次のコードを使用します。 json.dumps(jsonStr, sort_keys=False, ensure_ascii = False, indent=2))、ここでは、ensure_ascii が False である必要があることに注意する必要があります。そうでない場合、中国語が含まれている場合、0xx か何かが表示されます。indent=2 sort_keys は、この json を並べ替える必要がないことを意味します
#!/usr/bin/evn python3 #coding=utf-8 # 针对web端json协议的通信库,通信协议为json,传出的data为json格式,接收的数据也是json格式 # 外界调用时可先初始化web_json类,如下所示: # get调用 # web = web_json("http://baidu.com/") # params = "abcd/select/100000?userID=1234&groupID=79" # web.url_get(params) # # post调用 # web = web_json("http://baidu.com/") # params = "abcd/select/100000" # data = '{"name": "jack", "id": "1"}' # web.url_post(params, data) from urllib.request import urlopen from urllib.parse import quote import json class 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方法 def url_get(self, params): return self.get_url_data(params, None) # post方法 def url_post(self, params, data): data=bytes(data, 'utf8') return self.get_url_data(params, data)
以上が、皆さんの学習に役立つことを願っています。誰もが PHP 中国語 Web サイトをサポートするでしょう。
python3 で実装された Web 側の JSON 通信プロトコルに関する詳細な記事については、PHP 中国語 Web サイトに注目してください。

PythonArraysSupportVariousoperations:1)SlicingExtractsSubsets、2)Appending/ExtendingAdddesements、3)inSertingSelementSatspecificpositions、4)remvingingDeletesements、5)sorting/verversingsorder、and6)listenionsionsionsionsionscreatenewlistsebasedexistin

numpyarraysAressertialentionsionceivationsefirication-efficientnumericalcomputations andDatamanipulation.theyarecrucialindatascience、mashineelearning、物理学、エンジニアリング、および促進可能性への適用性、scaledatiencyを効率的に、forexample、infinancialanalyyy

UseanArray.ArrayOverAlistinPythonは、Performance-criticalCode.1)homogeneousdata:araysavememorywithpedelements.2)Performance-criticalcode:Araysofterbetterbetterfornumerumerumericaleperations.3)interf

いいえ、notallistoperationSaresuptedbyarrays、andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorintorintorinsertizizing、whosimpactsporformance.2)リスト

toaccesselementsinapythonlist、useindexing、negativeindexing、slicing、oriteration.1)indexingstartsat0.2)negativeindexingAcsesess.3)slicingextractStions.4)reterationSuseSuseSuseSuseSeSeS forLoopseCheckLentlentlentlentlentlentlenttodExeror。

Arraysinpython、特にvianumpy、arecrucialinscientificComputing fortheirefficienty andversitility.1)彼らは、fornumericaloperations、data analysis、andmachinelearning.2)numpy'simplementation incensuresfasteroperationsthanpasteroperations.3)arayableminablecickick

Pyenv、Venv、およびAnacondaを使用して、さまざまなPythonバージョンを管理できます。 1)Pyenvを使用して、複数のPythonバージョンを管理します。Pyenvをインストールし、グローバルバージョンとローカルバージョンを設定します。 2)VENVを使用して仮想環境を作成して、プロジェクトの依存関係を分離します。 3)Anacondaを使用して、データサイエンスプロジェクトでPythonバージョンを管理します。 4)システムレベルのタスク用にシステムPythonを保持します。これらのツールと戦略を通じて、Pythonのさまざまなバージョンを効果的に管理して、プロジェクトのスムーズな実行を確保できます。

numpyarrayshaveveraladvantages-averstandardpythonarrays:1)thealmuchfasterduetocベースのインプレンテーション、2)アレモレメモリ効率、特にlargedatasets、および3)それらは、拡散化された、構造化された形成術科療法、


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

Dreamweaver Mac版
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

Safe Exam Browser
Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。

EditPlus 中国語クラック版
サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

PhpStorm Mac バージョン
最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール

ホットトピック









