Python開發【第八篇】:socket網路編程,python第八篇

來源:互聯網
上載者:User

Python開發【第八篇】:socket網路編程,python第八篇

服務端:

用戶端:

服務端輸出:

用戶端輸出:

連續發送資訊:

服務端:

用戶端:

在linux上用戶端執行命令

服務端:

用戶端:

linux傳送檔案

服務端:

用戶端:

一次最多發送的資料量32768(不同系統有區別)

多次發送

服務端:

用戶端:

每互動一次檔案的變化:

socket實現簡單的ssh

服務端:

用戶端:

注意socket粘包問題。

方法一:在多個相連的send之間添加sleep。

方法二:在每次send之後進行一次互動。

服務端:

用戶端:

socket實現簡單的ftp

ftp server:

1、讀取檔案名稱

2、檢測檔案是否存在

3、開啟檔案

4、檢測檔案大小

5、傳送檔案大小給用戶端

6、等待用戶端確認

7、開始邊讀邊發資料

8、發送md5

服務端:

用戶端:

SocketServer

實現並發處理。

The socketserver module simplifies the task of writing network servers.

socketserver常用類型

class socketserver.TCPServer(server_address,RequestHandlerClass,bind_and_activate=True)

This uses the Internet TCP protocol,which provides for continuous streams of data between the client and server.

class socketserver.UDPServer(server_address,RequestHandlerClass,bind_and_activate=True)

This uses datagrams,which are discrete packets of information that may arrive out of order or be lost while in transit.The parameters are the same as for TCPServer.

建立一個socketserver至少分一下幾步:

1、You mast create a request handler class by subclassing the BaseRequestHandler class and overriding its handle() method;this method will process incoming requests.

建立一個請求處理類繼承BaseRequestHandler,並且重寫父類handle()。

2、You mast instantiate one of the server classes,passing it the server's address and the request handler class.

執行個體化TCPServer,並傳遞server ip和上述建立的請求處理類給這個TCPServer。

3、Then call the handle_request() or server_forever() method of the server object to process one or many requests.

server.handle_request():只處理一個請求

server.server_forever():處理多個請求

4、Call server_close() to close the socket.

服務端:

用戶端:

開啟一個新線程

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.