Python簡(jiǎn)單網(wǎng)絡(luò)編程示例【客戶(hù)端與服務(wù)端】
本文實(shí)例講述了Python簡(jiǎn)單網(wǎng)絡(luò)編程。分享給大家供大家參考,具體如下:
內(nèi)容目錄
1. 客戶(hù)端(client.py)
2. 服務(wù)端(server.py)
一、客戶(hù)端(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)
程序通過(guò)socket.socket()建立一個(gè)Socket,參數(shù)告訴系統(tǒng)需要一個(gè)Internet Socket進(jìn)行TCP通信。接著程序鏈接遠(yuǎn)程的主機(jī)名,并提供文件名。最后獲得響應(yīng)后在屏幕上打印出來(lái)。
測(cè)試
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 +
二、服務(wù)端(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) #每次最多只有一個(gè)等候處理
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()
建立一個(gè)socket,設(shè)置成可復(fù)用的(reusable),綁定端口號(hào)51421(可選大于1024的任一值),調(diào)用listen()函數(shù),開(kāi)始等待來(lái)自客戶(hù)端的請(qǐng)求,同時(shí)設(shè)定最多只有一個(gè)等候處理的鏈接。
主循環(huán)對(duì)a.accept()函數(shù)調(diào)用開(kāi)始,程序連接一個(gè)客戶(hù)端后立馬停止,接收用戶(hù)的輸入。
運(yùn)行一個(gè)例子
首先運(yùn)行server.py
python server.py
另開(kāi)一個(gè)終端,連接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.
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門(mén)與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- Python實(shí)現(xiàn)基于TCP UDP協(xié)議的IPv4 IPv6模式客戶(hù)端和服務(wù)端功能示例
- python thrift搭建服務(wù)端和客戶(hù)端測(cè)試程序
- python Socket之客戶(hù)端和服務(wù)端握手詳解
- Python警察與小偷的實(shí)現(xiàn)之一客戶(hù)端與服務(wù)端通信實(shí)例
- python網(wǎng)絡(luò)編程示例(客戶(hù)端與服務(wù)端)
- python實(shí)現(xiàn)socket客戶(hù)端和服務(wù)端簡(jiǎn)單示例
- python如何創(chuàng)建TCP服務(wù)端和客戶(hù)端
相關(guān)文章
Python編寫(xiě)memcached啟動(dòng)腳本代碼實(shí)例
這篇文章主要介紹了Python編寫(xiě)memcached啟動(dòng)腳本代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
在Python程序中實(shí)現(xiàn)分布式進(jìn)程的教程
這篇文章主要介紹了在Python程序中實(shí)現(xiàn)分布式進(jìn)程的教程,在多進(jìn)程編程中十分有用,示例代碼基于Python2.x版本,需要的朋友可以參考下2015-04-04
Python基礎(chǔ)之函數(shù)用法實(shí)例詳解
這篇文章主要介紹了Python中函數(shù)用法,包括了函數(shù)的創(chuàng)建、定義、參數(shù)等,需要的朋友可以參考下2014-09-09
Python發(fā)起請(qǐng)求提示UnicodeEncodeError錯(cuò)誤代碼解決方法
這篇文章主要介紹了Python發(fā)起請(qǐng)求提示UnicodeEncodeError錯(cuò)誤代碼解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
Python實(shí)現(xiàn)PS濾鏡中馬賽克效果示例
這篇文章主要介紹了Python實(shí)現(xiàn)PS濾鏡中馬賽克效果,涉及Python基于skimage庫(kù)的圖形馬賽克效果相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-01-01
解決pycharm的Python console不能調(diào)試當(dāng)前程序的問(wèn)題
今天小編就為大家分享一篇解決pycharm的Python console不能調(diào)試當(dāng)前程序的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01

