python網路程式設計證書 Python網路程式設計學習

2021-10-11 12:14:21 字數 2119 閱讀 9652

現今有關python網路程式設計的書籍很多,但是很多都是運用相關的package,之後直接從頂層來進行講授。但是這樣的學習方法比較適合技工,對於python愛好者來說,這種空中樓閣式的學習方法並沒有什麼益處。因為不同的package由程式設計風格迥然不同的人貢獻,最終導致的結果就是記住了一大堆亂七八糟的函式名,然而不知道為什麼要這樣用相應的函式,過了兩天也就全忘記了。從今天起,本文選擇一種從python內建的較底層的package開始學習的方式,逐步學習python的網路程式設計。越向底層進行學習,越能發現相應的問題。

今天首先開始對python3的socket包的源**開始學習。

socket包實現了一系列網路程式設計的工具函式。在未來一段時間中,我將選取一些socket.py中重要的**進行學習,之後用較底層的**實現一些簡單的網路程式設計功能。

我們從socket.py中定義的第乙個函式學起,如下:

try: import _sslexcept importerror: # no ssl support passelse: def

ssl(sock, keyfile=none, certfile=none): # we do an internal import here because

the ssl # module imports the socket module import ssl as _realssl

warnings.warn("socket.ssl() is deprecated. use ssl.wrap_socket() instead.",

deprecationwarning, stacklevel=2) return _realssl.sslwrap_******(sock, keyfile,

certfile) # we need to import the same constants we used to... from _ssl import

sslerror as sslerror from _ssl import \ rand_add, \ rand_egd, \ rand_status, \

ssl_error_zero_return, \ ssl_error_want_read, \ ssl_error_want_write, \

ssl_error_want_x509_lookup, \ ssl_error_syscall, \ ssl_error_ssl, \

ssl_error_want_connect, \ ssl_error_eof, \ ssl_error_invalid_error_code

try a except b else c

的語法是比較pythonic的,如果執行a**沒有捕獲到異常,那麼執行else之後的c段**,在上一段**中則是定義了ssl函式。我們重點關注下ssl()函式。

def ssl(sock, keyfile=none, certfile=none): # we do an internal import here

because the ssl # module imports the socket module import ssl as _realssl

warnings.warn("socket.ssl() is deprecated. use ssl.wrap_socket() instead.",

deprecationwarning, stacklevel=2) return _realssl.sslwrap_******(sock, keyfile,

certfile)

ssl全稱是secure sockets

layer,中文叫安全套接層。簡單來說就是當我們遠端與對方伺服器建立通訊時,需要獲得對方的認證,具體的認證方式不細說了,等到未來應用的時候再談。總之就是我們通過自身的keyfile(秘鑰)來獲得對方伺服器certfile(訪問證書)的過程。

這段**值得學習的地方有乙個internal import,作者說:

# we do an internal import here because the ssl # module imports the socket

module

因為ssl模組import了socket.py,如果放在外面顯然就會出現在socket模組**中import

socket**的奇怪情況,而將import socket 放在ssl函式的區域性作用域中就無此問題。

python網路程式設計 TCP網路程式設計

tcp程式設計 客戶端 import socket 1 套接字 tcp socket socket.socket socket.af inet,socket.sock stream 2 建立鏈結 tcp socket.connect 172.27.35.1 8080 3 傳送資訊 tcp socke...

python 網路程式設計

今天晚上學習了一下python的網路程式設計,實現了client向server傳送資料,server反饋資訊 python 3.3 版本 server from socket import class tcpserver object def init self,serverport self.se...

python網路程式設計

網路通訊是計算機之間的程序之間的通訊。tcp程式設計 tcp連線建立是雙向通道,客戶端與服務端都可以給對方傳送資料。建立tcp連線時,主動發起連線的叫客戶端,被動響應連線的叫服務端。建立乙個tcp的socket連線 用socket family,type 建立套接字獲得socket物件。family...