Python 網路通訊程式設計之套接字基本原理

2022-03-08 10:02:23 字數 899 閱讀 7305

socket()函式的引數有三個:

socket.af_inet:  預設ipv4。使用ipv4進行連線通訊。

socket.af_inet6:用於ipv6。使用ipv6進行連線通訊。

socket.af_unix:只能夠用於單一的unix系統程序間通訊。

socket.sock_stream:流式套接字,傳輸層基於tcp協議進行通訊。

socket.sock_dgram:資料報套接字,傳輸層基於udp協議進行通訊。

socket.sock_raw:     原始套接字,訪問底層協議的套接字。

socket.sock_rdm:     可靠udp形式。

socket.sock_seqpacket:可靠的連續資料報服務。

0:預設,可以省略。

can_raw或can_bcm:位址族為af_can時。

import socket

server = socket.socket()

server.setsockopt(socket.sol_socket,socket.so_reuseaddr,1) # 設定埠立即重用

print(server.getsockopt(socket.sol_socket,socket.so_reuseaddr)) # 獲取選項值:1

print(server.fileno()) # 套接字描述符:3

print(server.type) # 套接字型別:socketkind.sock_stream

server.bind(('127.0.0.1',3120))

server.listen(5)

conn, addr = server.accept()

Python 網路通訊程式設計之udp通訊程式設計

import socket 1.建立例項,即資料報套接字 server socket.socket socket.af inet,socket.sock dgram 2.繫結位址,進行監聽 server.bind 127.0.0.1 3120 3.收發訊息 while true data serve...

網路程式設計高階 非網路通訊套接字

1.非命名unix域套接字 linux下環境下使用socketpair函式創造一對未命名的,相互連線的unix域套接字 include int socketpair int domain,int type,int protocol,int sockfd 第乙個引數用於表示建立的套接字的域,本地程序通...

python網路通訊

import urllib 網路通訊模組 from urllib import request def url,ispicture false 預設引數 param url param ispicture return none,直接儲存為檔案,不需要返回值 file name url.split ...