亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python簡單網(wǎng)絡編程示例【客戶端與服務端】

 更新時間:2017年05月26日 11:36:01   作者:jihite  
這篇文章主要介紹了Python簡單網(wǎng)絡編程,詳細介紹了客戶端與服務端的具體實現(xiàn)技巧與相關注意事項,需要的朋友可以參考下

本文實例講述了Python簡單網(wǎng)絡編程。分享給大家供大家參考,具體如下:

內(nèi)容目錄

1. 客戶端(client.py)
2. 服務端(server.py)

一、客戶端(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()建立一個Socket,參數(shù)告訴系統(tǒng)需要一個Internet Socket進行TCP通信。接著程序鏈接遠程的主機名,并提供文件名。最后獲得響應后在屏幕上打印出來。

測試

python client.py quux.org /

顯示

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 + 

二、服務端(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()

建立一個socket,設置成可復用的(reusable),綁定端口號51421(可選大于1024的任一值),調(diào)用listen()函數(shù),開始等待來自客戶端的請求,同時設定最多只有一個等候處理的鏈接。

主循環(huán)對a.accept()函數(shù)調(diào)用開始,程序連接一個客戶端后立馬停止,接收用戶的輸入。

運行一個例子

首先運行server.py

python server.py

另開一個終端,連接localhost的51421端口。 

jihite@ubuntu:~/web$ telnet localhost 51421
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Welcome, ('127.0.0.1', 59853)
Please enter a string: mm
You entered 2 characters.
Connection closed by foreign host.

更多關于Python相關內(nèi)容感興趣的讀者可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對大家Python程序設計有所幫助。

相關文章

  • python跳出雙層for循環(huán)的解決方法

    python跳出雙層for循環(huán)的解決方法

    今天小編就為大家分享一篇python跳出雙層for循環(huán)的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • python靜態(tài)方法實例

    python靜態(tài)方法實例

    這篇文章主要介紹了python靜態(tài)方法,實例分析了python靜態(tài)方法的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-01-01
  • Python?enumerate()計數(shù)器簡化循環(huán)

    Python?enumerate()計數(shù)器簡化循環(huán)

    這篇文章主要介紹了Python?enumerate()計數(shù)器簡化循環(huán),enumerate()最大的優(yōu)點就是它返回一個帶有計數(shù)器和值的元組,因此我們不必自己增加計數(shù)器,下面就來看看文章具體對它的詳細介紹吧,需要的朋友可以參考一下
    2021-12-12
  • Python struct模塊解析

    Python struct模塊解析

    我們知道python只定義了6種數(shù)據(jù)類型,字符串,整數(shù),浮點數(shù),列表,元組,字典。但是C語言中有些字節(jié)型的變量,在python中該如何實現(xiàn)呢?這點頗為重要,特別是要在網(wǎng)絡上進行數(shù)據(jù)傳輸?shù)脑挕?/div> 2014-06-06
  • Django之使用celery和NGINX生成靜態(tài)頁面實現(xiàn)性能優(yōu)化

    Django之使用celery和NGINX生成靜態(tài)頁面實現(xiàn)性能優(yōu)化

    這篇文章主要介紹了Django之使用celery和NGINX生成靜態(tài)頁面實現(xiàn)性能優(yōu)化,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-10-10
  • python命令行工具Click快速掌握

    python命令行工具Click快速掌握

    這篇文章主要介紹了python命令行工具Click快速掌握,寫 Python 的經(jīng)常要寫一些命令行工具,雖然標準庫提供有命令行解析工具 Argparse,但是寫起來非常麻煩,我很少會使用它。命令行工具中用起來最爽的就是 Click,,需要的朋友可以參考下
    2019-07-07
  • python GUI實現(xiàn)小球滿屏亂跑效果

    python GUI實現(xiàn)小球滿屏亂跑效果

    這篇文章主要為大家詳細介紹了python GUI實現(xiàn)小球滿屏亂跑效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • Python中特殊函數(shù)集錦

    Python中特殊函數(shù)集錦

    這篇文章主要介紹了Python中特殊函數(shù),主要介紹一下四個函數(shù):1 過濾函數(shù)filter 2 映射和歸并函數(shù)map/reduce 3 裝飾器@ 4 匿名函數(shù)lamda,需要的朋友可以參考下
    2015-07-07
  • Django配合python進行requests請求的問題及解決方法

    Django配合python進行requests請求的問題及解決方法

    Python作為目前比較流行的編程語言,他內(nèi)置的Django框架就是一個很好的網(wǎng)絡框架,可以被用來搭建后端,和前端進行交互,那么我們現(xiàn)在來學習一下,如何用Python本地進行requests請求,并通過請求讓Django幫我們解決一些問題
    2022-06-06
  • Python列表list的詳細用法介紹

    Python列表list的詳細用法介紹

    這篇文章主要介紹了Python列表list的詳細用法介紹,列表(list)作為Python中基本的數(shù)據(jù)結(jié)構(gòu),是存儲數(shù)據(jù)的容器,相當于其它語言中所說的數(shù)組
    2022-07-07

最新評論