国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python簡單網絡編程示例【客戶端與服務端】

2019-11-25 16:08:48
字體:
來源:轉載
供稿:網友

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

內容目錄

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

一、客戶端(client.py)

import socketimport sysport = 70host = 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,參數告訴系統需要一個Internet Socket進行TCP通信。接著程序鏈接遠程的主機名,并提供文件名。最后獲得響應后在屏幕上打印出來。

測試

python client.py quux.org /

顯示

iWelcome to gopher at quux.org! fake  (NULL) 0i  fake  (NULL) 0iThis server has a lot of information of historic interest, fake  (NULL) 0ifunny, or just plain entertaining -- all presented in Gopher. fake  (NULL) 0iThere are many mirrors here of rare or valuable files with the fake  (NULL) 0iaim to preserve them in case their host disappears. PLEASE READ  fake  (NULL) 0i"About This Server" FOR IMPORTANT NOTES AND LEGAL INFORMATION. fake  (NULL) 0i  fake  (NULL) 00About 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) 0i"We the Media: Grassroots Journalism by the People, for the People"  fake  (NULL) 0iby Dan Gillmor in various formats. fake  (NULL) 0i  fake  (NULL) 0iFeel free to download and enjoy.  fake  (NULL) 01Computers /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 701Government, 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 701Internet  /Internet  gopher.quux.org 70 +1Other Gopher Servers  /Software/Gopher/servers  gopher.quux.org 701People /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 700What's New /whatsnew.txt  gopher.quux.org 70 + 

二、服務端(server.py)

# coding: utf-8import sockethost = ''port = 51421s = 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." %portwhile 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的任一值),調用listen()函數,開始等待來自客戶端的請求,同時設定最多只有一個等候處理的鏈接。

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

運行一個例子

首先運行server.py

python server.py

另開一個終端,連接localhost的51421端口?!?/p>

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

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

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 白水县| 秭归县| 绩溪县| 工布江达县| 凌源市| 清涧县| 汪清县| 隆回县| 称多县| 洛扎县| 乌兰察布市| 谷城县| 翼城县| 海口市| 建始县| 达州市| 射洪县| 来安县| 太谷县| 佛坪县| 渭南市| 阿克| 伊金霍洛旗| 上饶县| 剑河县| 松桃| 根河市| 原平市| 施甸县| 德庆县| 嘉鱼县| 儋州市| 吕梁市| 吉首市| 庐江县| 通海县| 科技| 长宁县| 淄博市| 龙岩市| 奉新县|