ホームページ  >  記事  >  バックエンド開発  >  Python クライアントとサーバーの簡単なネットワーク プログラミングの例

Python クライアントとサーバーの簡単なネットワーク プログラミングの例

黄舟
黄舟オリジナル
2017-05-28 11:11:081890ブラウズ

この記事では、主にPython簡単なネットワークプログラミングを紹介し、クライアントとサーバーの具体的な実装スキルと関連する注意事項を詳しく紹介します。必要な友人はそれを参照してください

この記事では、Pythonの簡単なネットワークプログラミングについて説明します。例。参考までに皆さんと共有します。詳細は次のとおりです:

コンテンツディレクトリ

1. クライアント (client.py)
2. サーバー (server.py)

1. クライアント (client.py) )

import socket
import sys
port = 70
host = sys.argv[1]
filename = sys.argv[2]
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
fd = s.makefile("rw", 0)
fd.write(filename + "\n")
for line in fd.readlines():
  sys.stdout.write(line)

プログラムは、socket.socket() を通じてソケットを作成し、TCP 通信にインターネット ソケットが必要であることをシステムに伝えます。次に、プログラムはリモート ホスト名に接続し、ファイル名を提供します。最終的に応答を取得したら、それを画面に印刷します。

テスト

python client.py quux.org /

Display

iWelcome to gopher at quux.org! fake  (NULL) 0
i  fake  (NULL) 0
iThis server has a lot of information of historic interest, fake  (NULL) 0
ifunny, or just plain entertaining -- all presented in Gopher. fake  (NULL) 0
iThere are many mirrors here of rare or valuable files with the fake  (NULL) 0
iaim to preserve them in case their host disappears. PLEASE READ  fake  (NULL) 0
i"About This Server" FOR IMPORTANT NOTES AND LEGAL INFORMATION. fake  (NULL) 0
i  fake  (NULL) 0
0About This Server /About This Server.txt gopher.quux.org 70 +
1Archives  /Archives  gopher.quux.org 70 +
1Books /Books gopher.quux.org 70 +
1Communication /Communication gopher.quux.org 70 +
iThis directory contains the entire text of the book  fake  (NULL) 0
i"We the Media: Grassroots Journalism by the People, for the People"  fake  (NULL) 0
iby Dan Gillmor in various formats. fake  (NULL) 0
i  fake  (NULL) 0
iFeel free to download and enjoy.  fake  (NULL) 0
1Computers /Computers gopher.quux.org 70 +
1Current Issues and Events (Updated Apr. 23, 2002) /Current  gopher.quux.org 70 +
1Development Projects  /devel gopher.quux.org 70 +
0Gopher's 10th Anniversary /3.0.0.txt gopher.quux.org 70
1Government, Politics, Law, and Conflict  /Government gopher.quux.org 70 +
0How To Help  /How To Help.txt  gopher.quux.org 70 +
1Humor and Fun /Humor and Fun gopher.quux.org 70 +
1Index to Quux.Org /Archives/index gopher.quux.org 70
1Internet  /Internet  gopher.quux.org 70 +
1Other Gopher Servers  /Software/Gopher/servers  gopher.quux.org 70
1People /People gopher.quux.org 70 +
1Reference /Reference gopher.quux.org 70 +
1Software and Downloads /Software  gopher.quux.org 70 +
1The Gopher Project /Software/Gopher  gopher.quux.org 70
0What's New /whatsnew.txt  gopher.quux.org 70 + 

2. サーバー (server.py)

# coding: utf-8
import socket
host = ''
port = 51421
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))
s.listen(1)        #每次最多只有一个等候处理
print "Server is running on port %d; press Ctrl-C to terminate." %port
while 1:
  clientsock, clientaddr = s.accept()
  clientfile = clientsock.makefile('rw', 0)
  clientfile.write("Welcome, " + str(clientaddr) + "\n")
  clientfile.write("Please enter a string: ")
  line = clientfile.readline().strip()
  clientfile.write("You entered %d characters. \n" %len(line))
  clientfile.close()
  clientsock.close()

ソケットを作成し、再利用可能 (再利用可能) に設定し、ポート番号 51421 (オプションで 1024 より大きい) をバインドします。 value) を呼び出し、listen() 関数 を呼び出してクライアントからのリクエストの待機を開始し、処理を待機するリンクを最大 1 つ設定します。

メイン ループは、a.accept() 関数を呼び出すことによって開始され、プログラムはクライアントに接続してユーザー入力を受け取るとすぐに停止します。

サンプルを実行します

最初にserver.pyを実行します

python server.py

別のターミナルを開き、localhostのポート51421に接続します。

りー

以上がPython クライアントとサーバーの簡単なネットワーク プログラミングの例の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。