Ổ cắm thô Python gửi gói IP
Mô-đun này cung cấp quyền truy cập vào giao diện ổ cắm BSD. Nó có sẵn trên tất cả các hệ thống Unix, Windows, MacOS hiện đại và có thể là các nền tảng bổ sung Show
Ghi chú Một số hành vi có thể phụ thuộc vào nền tảng, vì các cuộc gọi được thực hiện tới các API socket của hệ điều hành không phải Emscripten, không phải WASI Mô-đun này không hoạt động hoặc không khả dụng trên nền tảng WebAssugging import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)6 và import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)7. Xem để biết thêm thông tin Giao diện Python là sự chuyển ngữ đơn giản của lệnh gọi hệ thống Unix và giao diện thư viện cho các ổ cắm sang kiểu hướng đối tượng của Python. hàm import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)8 trả về một đối tượng ổ cắm có các phương thức thực hiện các lời gọi hệ thống ổ cắm khác nhau. Parameter types are somewhat higher-level than in the C interface. as with import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)9 and >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]0 operations on Python files, buffer allocation on receive operations is automatic, and buffer length is implicit on send operations See also ModuleClasses that simplify writing network servers ModuleA TLS/SSL wrapper for socket objects Socket familiesDepending on the system and the build options, various socket families are supported by this module The address format required by a particular socket object is automatically selected based on the address family specified when the socket object was created. Socket addresses are represented as follows
Nếu bạn sử dụng tên máy chủ trong phần máy chủ của địa chỉ ổ cắm IPv4/v6, thì chương trình có thể hiển thị hành vi không xác định, vì Python sử dụng địa chỉ đầu tiên được trả về từ độ phân giải DNS. Địa chỉ ổ cắm sẽ được phân giải khác thành địa chỉ IPv4/v6 thực tế, tùy thuộc vào kết quả từ độ phân giải DNS và/hoặc cấu hình máy chủ. Đối với hành vi xác định, hãy sử dụng địa chỉ số trong phần máy chủ Tất cả các lỗi tăng ngoại lệ. Các ngoại lệ thông thường đối với các loại đối số không hợp lệ và điều kiện hết bộ nhớ có thể được nêu ra. Các lỗi liên quan đến ngữ nghĩa của ổ cắm hoặc địa chỉ hoặc một trong các lớp con của nó Chế độ không chặn được hỗ trợ thông qua. Việc tổng quát hóa điều này dựa trên thời gian chờ được hỗ trợ thông qua nội dung mô-đunMô-đun xuất các phần tử sau ngoại lệngoại lệ ổ cắm. lỗiMột bí danh không dùng nữa của Đã thay đổi trong phiên bản 3. 3. Sau PEP 3151, lớp này được đặt bí danh là. ngoại lệ ổ cắm. lỗiMột lớp con của , ngoại lệ này được đưa ra đối với các lỗi liên quan đến địa chỉ, tôi. e. đối với các chức năng sử dụng h_errno trong API POSIX C, bao gồm và. Giá trị đi kèm là một cặp import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)20 đại diện cho một lỗi được trả về bởi lệnh gọi thư viện. h_errno là một giá trị số, trong khi chuỗi đại diện cho mô tả của h_errno, được trả về bởi hàm import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)21 C Đã thay đổi trong phiên bản 3. 3. Lớp này được tạo thành lớp con của. ngoại lệ ổ cắm. gaierrorMột lớp con của , ngoại lệ này được đưa ra đối với các lỗi liên quan đến địa chỉ bởi và. Giá trị đi kèm là một cặp import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)26 đại diện cho một lỗi được trả về bởi lệnh gọi thư viện. chuỗi đại diện cho mô tả lỗi, như được trả về bởi hàm import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)27 C. Giá trị lỗi số sẽ khớp với một trong các hằng số import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)28 được xác định trong mô-đun này Đã thay đổi trong phiên bản 3. 3. Lớp này được tạo thành lớp con của. exception socket. hết giờMột bí danh không dùng nữa của Một lớp con của , ngoại lệ này được đưa ra khi thời gian chờ xảy ra trên ổ cắm đã hết thời gian chờ được bật thông qua lệnh gọi trước tới (hoặc ngầm định thông qua ). Giá trị đi kèm là một chuỗi có giá trị hiện tại luôn ở trạng thái “timed out” Đã thay đổi trong phiên bản 3. 3. Lớp này được tạo thành lớp con của. Đã thay đổi trong phiên bản 3. 10. Lớp này được đặt bí danh là. hằng sốổ cắm. AF_UNIXổ cắm. AF_INETổ cắm. AF_INET6 Các hằng số này đại diện cho các họ địa chỉ (và giao thức), được sử dụng cho đối số đầu tiên của import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)8. Nếu hằng số không được xác định thì giao thức này không được hỗ trợ. Có thể có nhiều hằng số hơn tùy thuộc vào hệ thốngổ cắm. SOCK_STREAMổ cắm. SOCK_DGRAMổ cắm. SOCK_RAWổ cắm. SOCK_RDMổ cắm. SOCK_SEQPACKET Các hằng số này đại diện cho các loại ổ cắm, được sử dụng cho đối số thứ hai của import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)8. Có thể có nhiều hằng số hơn tùy thuộc vào hệ thống. (Chỉ và dường như là hữu ích nói chung. )ổ cắm. SOCK_CLOEXECổ cắm. SOCK_NONBLOCK Hai hằng số này, nếu được xác định, có thể được kết hợp với các loại ổ cắm và cho phép bạn đặt một số cờ theo nguyên tắc (do đó tránh được các điều kiện tranh chấp có thể xảy ra và nhu cầu gọi riêng) See also Xử lý mô tả tệp an toàn để được giải thích kỹ lưỡng hơn Linux >= 2. 6. 27 Mới trong phiên bản 3. 2 SO_*ổ cắm. SOMAXCONNMSG_*SOL_*SCM_*IPPROTO_*IPPORT_*INADDR_*IP_*IPV6_*EAI_*AI_*NI_*TCP_*Nhiều hằng số của các dạng này, được ghi lại trong tài liệu Unix về ổ cắm và/hoặc giao thức IP, cũng được xác định trong mô-đun ổ cắm. Chúng thường được sử dụng trong các đối số cho các phương thức import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)44 và import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)45 của các đối tượng ổ cắm. Trong hầu hết các trường hợp, chỉ những ký hiệu được xác định trong tệp tiêu đề Unix mới được xác định; Đã thay đổi trong phiên bản 3. 6. _______0_______46, import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)47, import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)48, import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)49, import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)50, import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)51 đã được thêm vào. Đã thay đổi trong phiên bản 3. 6. 5. Trên Windows, import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)52, import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)53 xuất hiện nếu Windows hỗ trợ thời gian chạy. Đã thay đổi trong phiên bản 3. 7. ______0_______54 đã được thêm vào. Trên Windows, import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)55, import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)56 xuất hiện nếu thời gian chạy Windows hỗ trợ Đã thay đổi trong phiên bản 3. 10. ______0_______57 đã được thêm vào. Đã thêm import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)58. Trên MacOS, hằng số này có thể được sử dụng giống như cách mà import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)55 được sử dụng trên Linux. Đã thay đổi trong phiên bản 3. 11. Đã thêm import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)60. Trên MacOS, hằng số này có thể được sử dụng giống như cách mà import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)61 được sử dụng trên Linux và BSD. socket. AF_CANổ cắm. PF_CANSOL_CAN_*CAN_* Nhiều hằng số của các dạng này, được ghi lại trong tài liệu Linux, cũng được định nghĩa trong mô-đun ổ cắm Linux >= 2. 6. 25, NetBSD >= 8 Mới trong phiên bản 3. 3 Đã thay đổi trong phiên bản 3. 11. Đã thêm hỗ trợ NetBSD. ổ cắm. CAN_BCMCAN_BCM_*CAN_BCM, trong họ giao thức CAN, là giao thức quản lý quảng bá (BCM). Các hằng số của trình quản lý phát sóng, được ghi lại trong tài liệu Linux, cũng được xác định trong mô-đun ổ cắm Linux >= 2. 6. 25 Ghi chú Cờ import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)62 chỉ khả dụng trên Linux >= 4. 8 Mới trong phiên bản 3. 4 ổ cắm. CAN_RAW_FD_FRAMESCho phép hỗ trợ CAN FD trong ổ cắm CAN_RAW. Điều này bị tắt theo mặc định. Điều này cho phép ứng dụng của bạn gửi cả khung CAN và CAN FD; Hằng số này được ghi lại trong tài liệu Linux Linux >= 3. 6 Mới trong phiên bản 3. 5 ổ cắm. CAN_RAW_JOIN_FILTERSTham gia các bộ lọc CAN được áp dụng sao cho chỉ các khung CAN khớp với tất cả các bộ lọc CAN đã cho mới được chuyển đến không gian người dùng Hằng số này được ghi lại trong tài liệu Linux Linux >= 4. 1 Mới trong phiên bản 3. 9 ổ cắm. CAN_ISOTPCAN_ISOTP, trong họ giao thức CAN, là giao thức ISO-TP (ISO 15765-2). Các hằng số ISO-TP, được ghi lại trong tài liệu Linux Linux >= 2. 6. 25 Mới trong phiên bản 3. 7 ổ cắm. CAN_J1939CAN_J1939, trong họ giao thức CAN, là giao thức SAE J1939. Các hằng số J1939, được ghi lại trong tài liệu Linux Linux >= 5. 4 Mới trong phiên bản 3. 9 ổ cắm. AF_PACKETổ cắm. PF_PACKETPACKET_*Nhiều hằng số của các dạng này, được ghi lại trong tài liệu Linux, cũng được định nghĩa trong mô-đun ổ cắm Linux >= 2. 2 ổ cắm. AF_RDSổ cắm. ổ cắm PF_RDS. SOL_RDSRDS_*Nhiều hằng số của các dạng này, được ghi lại trong tài liệu Linux, cũng được định nghĩa trong mô-đun ổ cắm Linux >= 2. 6. 30 Mới trong phiên bản 3. 3 ổ cắm. SIO_RCVALLổ cắm. SIO_KEEPALIVE_VALSổ cắm. SIO_LOOPBACK_FAST_PATHRCVALL_*Các hằng số cho WSAIoctl() của Windows. Các hằng số được sử dụng làm đối số cho phương thức của các đối tượng socket Đã thay đổi trong phiên bản 3. 6. ______0_______64 đã được thêm vào. MẸO_*Các hằng số liên quan đến TIPC, khớp với các hằng số được xuất bởi API ổ cắm C. See the TIPC documentation for more information ổ cắm. AF_ALGổ cắm. SOL_ALGALG_*Các hằng số cho mã hóa hạt nhân Linux Linux >= 2. 6. 38 Mới trong phiên bản 3. 6 ổ cắm. AF_VSOCKổ cắm. IOCTL_VM_SOCKETS_GET_LOCAL_CIDVMADDR*SO_VM*Các hằng số cho giao tiếp máy chủ/khách Linux Linux >= 4. 8 Mới trong phiên bản 3. 7 ổ cắm. AF_LINKBSD, macOS Mới trong phiên bản 3. 4 ổ cắm. has_ipv6Hằng số này chứa một giá trị boolean cho biết liệu IPv6 có được hỗ trợ trên nền tảng này hay không ổ cắm. BDADDR_ANYổ cắm. BDADDR_LOCALĐây là các hằng chuỗi chứa địa chỉ Bluetooth có ý nghĩa đặc biệt. Ví dụ: có thể được sử dụng để chỉ ra bất kỳ địa chỉ nào khi chỉ định ổ cắm liên kết với # Echo client program import socket HOST = 'daring.cwi.nl' # The remote host PORT = 50007 # The same port as used by the server with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect((HOST, PORT)) s.sendall(b'Hello, world') data = s.recv(1024) print('Received', repr(data))1ổ cắm. HCI_FILTERổ cắm. ổ cắm HCI_TIME_STAMP. HCI_DATA_DIR Để sử dụng với # Echo client program import socket HOST = 'daring.cwi.nl' # The remote host PORT = 50007 # The same port as used by the server with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect((HOST, PORT)) s.sendall(b'Hello, world') data = s.recv(1024) print('Received', repr(data))5. không có sẵn cho NetBSD hoặc DragonFlyBSD. và không khả dụng cho FreeBSD, NetBSD hoặc DragonFlyBSDổ cắm. AF_QIPCRTR Hằng số cho giao thức bộ định tuyến IPC của Qualcomm, được sử dụng để giao tiếp với dịch vụ cung cấp bộ xử lý từ xa Linux >= 4. 7 ổ cắm. SCM_CREDS2ổ cắm. LOCAL_CREDSổ cắm. LOCAL_CREDS_PERSISTENTLOCAL_CREDS và LOCAL_CREDS_PERSISTENT có thể được sử dụng với ổ cắm SOCK_DGRAM, SOCK_STREAM, tương đương với Linux/DragonFlyBSD SO_PASSCRED, trong khi LOCAL_CREDS gửi thông tin đăng nhập ở lần đọc đầu tiên, LOCAL_CREDS_PERSISTENT gửi cho mỗi lần đọc, SCM_CREDS2 sau đó phải được sử dụng cho lần đọc sau đối với loại thông báo Mới trong phiên bản 3. 11 BSD miễn phí ổ cắm. SO_INCOMING_CPU
Mới trong phiên bản 3. 11 Linux >= 3. 9 Chức năngTạo ổ cắmThe following functions all create lớp . ổ cắm(gia đình=AF_INET, type=SOCK_STREAM, proto=0, fileno=None)Tạo một ổ cắm mới bằng cách sử dụng họ địa chỉ, loại ổ cắm và số giao thức đã cho. Họ địa chỉ phải là (mặc định), , , , hoặc. Loại ổ cắm phải là (mặc định), hoặc có lẽ là một trong các hằng số import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)81 khác. Số giao thức thường bằng 0 và có thể bị bỏ qua hoặc trong trường hợp họ địa chỉ là giao thức thì giao thức phải là một trong số import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)83, , hoặc Nếu fileno được chỉ định, các giá trị cho họ, loại và nguyên mẫu được tự động phát hiện từ bộ mô tả tệp đã chỉ định. Tự động phát hiện có thể bị ghi đè bằng cách gọi hàm với các đối số họ, loại hoặc proto rõ ràng. Điều này chỉ ảnh hưởng đến cách Python đại diện cho e. g. giá trị trả về nhưng không phải là tài nguyên hệ điều hành thực tế. Không giống như, fileno sẽ trả về cùng một ổ cắm và không trùng lặp. Điều này có thể giúp đóng một ổ cắm tách rời bằng cách sử dụng Ổ cắm mới được tạo là Tăng một import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)90 với các đối số import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)91, import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)92, import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)93, import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)94 Đã thay đổi trong phiên bản 3. 3. Dòng AF_CAN đã được thêm vào. Họ AF_RDS đã được thêm vào. Đã thay đổi trong phiên bản 3. 4. Giao thức CAN_BCM đã được thêm vào. Đã thay đổi trong phiên bản 3. 4. Ổ cắm được trả về hiện không thể kế thừa. Đã thay đổi trong phiên bản 3. 7. Giao thức CAN_ISOTP đã được thêm vào. Đã thay đổi trong phiên bản 3. 7. Khi hoặc cờ bit được áp dụng cho loại, chúng sẽ bị xóa và sẽ không phản ánh chúng. Chúng vẫn được chuyển đến hệ thống cơ bản import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)8 cuộc gọi. Vì vậy, sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM | socket.SOCK_NONBLOCK) vẫn sẽ tạo non-blocking socket trên các hệ điều hành hỗ trợ import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)95, nhưng >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]00 sẽ được đặt thành >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]01 Đã thay đổi trong phiên bản 3. 9. Giao thức CAN_J1939 đã được thêm vào. Đã thay đổi trong phiên bản 3. 10. Giao thức IPPROTO_MPTCP đã được thêm vào. ổ cắm. cặp ổ cắm([gia đình[, type[, proto]]])Xây dựng một cặp đối tượng ổ cắm được kết nối bằng cách sử dụng họ địa chỉ, loại ổ cắm và số giao thức đã cho. Họ địa chỉ, loại ổ cắm và số giao thức giống như hàm import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)8 ở trên. Họ mặc định là nếu được xác định trên nền tảng; Các ổ cắm mới được tạo là Đã thay đổi trong phiên bản 3. 2. Các đối tượng ổ cắm được trả về hiện hỗ trợ toàn bộ API ổ cắm, thay vì một tập hợp con. Đã thay đổi trong phiên bản 3. 4. Các ổ cắm được trả lại hiện không thể kế thừa. Đã thay đổi trong phiên bản 3. 5. Đã thêm hỗ trợ Windows. ổ cắm. create_connection(địa chỉ , thời gian chờ=GLOBAL_DEFAULT, source_address=None, *, all_errors=False)Kết nối với dịch vụ TCP đang nghe trên địa chỉ internet (2-tuple >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]6) và trả về đối tượng ổ cắm. Đây là một chức năng cấp cao hơn so với. if host is a non-numeric hostname, it will try to resolve it for both and , and then try to connect to all possible addresses in turn until a connection succeeds. Điều này giúp dễ dàng viết các ứng dụng khách tương thích với cả IPv4 và IPv6 Truyền tham số thời gian chờ tùy chọn sẽ đặt thời gian chờ trên phiên bản ổ cắm trước khi thử kết nối. Nếu không có thời gian chờ nào được cung cấp, cài đặt thời gian chờ mặc định chung được trả về sẽ được sử dụng Nếu được cung cấp, source_address phải là 2-tuple >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]6 để ổ cắm liên kết thành địa chỉ nguồn của nó trước khi kết nối. Nếu máy chủ hoặc cổng tương ứng là ‘’ hoặc 0 thì hành vi mặc định của hệ điều hành sẽ được sử dụng Khi không thể tạo kết nối, một ngoại lệ được đưa ra. Theo mặc định, nó là ngoại lệ từ địa chỉ cuối cùng trong danh sách. Nếu all_errors là >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]11, thì nó chứa lỗi của tất cả các lần thử Đã thay đổi trong phiên bản 3. 2. source_address đã được thêm. Đã thay đổi trong phiên bản 3. 11. all_errors đã được thêm vào. ổ cắm. create_server(địa chỉ , *, family=AF_INET, backlog=None, reuse_port=False, dualstack_ipv6=False)Hàm tiện lợi tạo ổ cắm TCP được liên kết với địa chỉ (2-tuple >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]6) và trả về đối tượng ổ cắm gia đình nên là một trong hai hoặc. tồn đọng là kích thước hàng đợi được chuyển đến; . tái sử dụng_port cho biết có nên đặt tùy chọn ổ cắm import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)71 hay không Nếu dualstack_ipv6 là đúng và nền tảng hỗ trợ thì ổ cắm sẽ có thể chấp nhận cả kết nối IPv4 và IPv6, nếu không thì nó sẽ tăng. Hầu hết các nền tảng POSIX và Windows được cho là hỗ trợ chức năng này. Khi chức năng này được bật, địa chỉ được trả về khi xảy ra kết nối IPv4 sẽ là địa chỉ IPv6 được biểu thị dưới dạng địa chỉ IPv6 được ánh xạ IPv4. Nếu dualstack_ipv6 là false, nó sẽ vô hiệu hóa chức năng này một cách rõ ràng trên các nền tảng kích hoạt nó theo mặc định (e. g. Linux). Tham số này có thể được sử dụng kết hợp với import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr) Ghi chú Trên nền tảng POSIX, tùy chọn ổ cắm >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]21 được đặt để sử dụng lại ngay các ổ cắm trước đó được liên kết trên cùng một địa chỉ và duy trì ở trạng thái TIME_WAIT Mới trong phiên bản 3. 8 ổ cắm. has_dualstack_ipv6()Trả lại >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]11 nếu nền tảng hỗ trợ tạo ổ cắm TCP có thể xử lý cả kết nối IPv4 và IPv6 Mới trong phiên bản 3. 8 ổ cắm. từfd(fd , gia đình, type, proto=0)Sao chép bộ mô tả tệp fd (một số nguyên được trả về bởi phương thức >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]23 của đối tượng tệp) và xây dựng một đối tượng ổ cắm từ kết quả. Họ địa chỉ, loại ổ cắm và số giao thức giống như hàm import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)8 ở trên. Bộ mô tả tệp phải tham chiếu đến một ổ cắm, nhưng điều này không được chọn — các thao tác tiếp theo trên đối tượng có thể thất bại nếu bộ mô tả tệp không hợp lệ. Chức năng này hiếm khi cần thiết, nhưng có thể được sử dụng để nhận hoặc đặt các tùy chọn ổ cắm trên một ổ cắm được chuyển đến chương trình dưới dạng đầu vào hoặc đầu ra tiêu chuẩn (chẳng hạn như máy chủ được khởi động bởi Unix inet daemon). Ổ cắm được coi là ở chế độ chặn Ổ cắm mới được tạo là Đã thay đổi trong phiên bản 3. 4. Ổ cắm được trả về hiện không thể kế thừa. ổ cắm. từ chia sẻ(dữ liệu)Khởi tạo một ổ cắm từ dữ liệu thu được từ phương thức. Ổ cắm được coi là ở chế độ chặn các cửa sổ Mới trong phiên bản 3. 3 ổ cắm. Loại ổ cắmĐây là một đối tượng kiểu Python đại diện cho kiểu đối tượng ổ cắm. Nó giống như >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]26 Cac chưc năng khacMô-đun này cũng cung cấp các dịch vụ liên quan đến mạng khác nhau ổ cắm. đóng(fd)Đóng bộ mô tả tệp ổ cắm. Điều này giống như , nhưng đối với ổ cắm. Trên một số nền tảng (đáng chú ý nhất là Windows) không hoạt động đối với bộ mô tả tệp ổ cắm Mới trong phiên bản 3. 7 ổ cắm. getaddrinfo(máy chủ , cổng, family=0, type=0, proto=0, flags=0)Dịch đối số máy chủ/cổng thành một chuỗi gồm 5 bộ chứa tất cả các đối số cần thiết để tạo ổ cắm được kết nối với dịch vụ đó. host is a domain name, a string representation of an IPv4/v6 address or >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]30. cổng là tên dịch vụ chuỗi, chẳng hạn như >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]31, số cổng dạng số hoặc >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]30. Bằng cách chuyển >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]30 làm giá trị của máy chủ và cổng, bạn có thể chuyển >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]34 cho API C bên dưới Các đối số họ, loại và nguyên mẫu có thể được chỉ định tùy chọn để thu hẹp danh sách địa chỉ được trả về. Việc chuyển số 0 làm giá trị cho từng đối số này sẽ chọn toàn bộ phạm vi kết quả. Đối số flags có thể là một hoặc một vài trong số >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]35 hằng số và sẽ ảnh hưởng đến cách tính toán và trả về kết quả. Ví dụ: >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]36 sẽ vô hiệu hóa độ phân giải tên miền và sẽ gây ra lỗi nếu máy chủ lưu trữ là một tên miền Hàm trả về danh sách 5 bộ có cấu trúc sau >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]37 Trong các bộ này, family, type, proto đều là số nguyên và được truyền cho hàm import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)8. canonname sẽ là một chuỗi đại diện cho tên chuẩn của máy chủ lưu trữ nếu >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]39 là một phần của đối số flags; . sockaddr là một bộ mô tả một địa chỉ ổ cắm, có định dạng phụ thuộc vào họ được trả về (một bộ 2 của >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]40 cho , một bộ 4 của >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]42 cho ) và được truyền cho phương thức Nâng cao một >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]45 với các đối số >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]46, >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]47, import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)92, import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)93, import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)94 Ví dụ sau tìm nạp thông tin địa chỉ cho một kết nối TCP giả định tới >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]51 trên cổng 80 (kết quả có thể khác trên hệ thống của bạn nếu IPv6 không được bật) >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))] Đã thay đổi trong phiên bản 3. 2. các tham số hiện có thể được chuyển bằng các đối số từ khóa. Đã thay đổi trong phiên bản 3. 7. đối với địa chỉ multicast IPv6, chuỗi biểu thị địa chỉ sẽ không chứa phần >>> import socket >>> s1, s2 = socket.socketpair() >>> b1 = bytearray(b'----') >>> b2 = bytearray(b'0123456789') >>> b3 = bytearray(b'--------------') >>> s1.send(b'Mary had a little lamb') 22 >>> s2.recvmsg_into([b1, memoryview(b2)[2:9], b3]) (22, [], 0, None) >>> [b1, b2, b3] [bytearray(b'Mary'), bytearray(b'01 had a 9'), bytearray(b'little lamb---')]0. ổ cắm. getfqdn([tên]) Trả lại một tên miền đủ điều kiện cho tên. Nếu tên bị bỏ qua hoặc trống, nó được hiểu là máy chủ lưu trữ cục bộ. Để tìm tên đủ điều kiện, tên máy chủ được trả về được chọn, theo sau là bí danh cho máy chủ, nếu có. Tên đầu tiên bao gồm một khoảng thời gian được chọn. Trong trường hợp không có tên miền đủ điều kiện và tên đã được cung cấp, nó sẽ được trả về không thay đổi. Nếu tên trống hoặc bằng >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]54, tên máy chủ từ được trả vềổ cắm. gethostbyname(tên máy chủ) Dịch tên máy chủ sang định dạng địa chỉ IPv4. Địa chỉ IPv4 được trả về dưới dạng một chuỗi, chẳng hạn như >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]9. Nếu tên máy chủ là một địa chỉ IPv4, nó được trả về không thay đổi. Xem để có giao diện đầy đủ hơn. không hỗ trợ độ phân giải tên IPv6 và nên được sử dụng thay thế cho hỗ trợ ngăn xếp kép IPv4/v6 Tăng một >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]60 với đối số >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]61 không phải WASI ổ cắm. gethostbyname_ex(tên máy chủ)Dịch tên máy chủ sang định dạng địa chỉ IPv4, giao diện mở rộng. Trả về một bộ ba >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]62 trong đó tên máy chủ là tên máy chủ chính của máy chủ lưu trữ, danh sách bí danh là danh sách (có thể trống) các tên máy chủ thay thế cho cùng một địa chỉ và ipaddrlist là danh sách các địa chỉ IPv4 cho cùng một giao diện trên cùng một máy chủ (thường nhưng không . không hỗ trợ độ phân giải tên IPv6 và nên được sử dụng thay thế cho hỗ trợ ngăn xếp kép IPv4/v6 Tăng một >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]60 với đối số >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]61 không phải WASI ổ cắm. gethostname()Trả về một chuỗi chứa tên máy chủ mà trình thông dịch Python hiện đang thực thi Tăng >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]67 mà không có đối số Ghi chú. không phải lúc nào cũng trả lại tên miền đủ điều kiện; không phải WASI ổ cắm. gethostbyaddr(ip_address)Trả về bộ ba >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]62 trong đó tên máy chủ là tên máy chủ chính phản hồi ip_address đã cho, aliaslist là danh sách (có thể trống) các tên máy chủ thay thế cho cùng một địa chỉ và ipaddrlist là danh sách các địa chỉ IPv4/v6 cho cùng một giao diện trên . Để tìm tên miền đủ điều kiện, hãy sử dụng chức năng. hỗ trợ cả IPv4 và IPv6 Đưa ra một >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]73 với lập luận >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]74 không phải WASI ổ cắm. getnameinfo(sockaddr , cờ)Dịch địa chỉ ổ cắm sockaddr thành 2-tuple >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]6. Tùy thuộc vào cài đặt của cờ, kết quả có thể chứa một tên miền đủ điều kiện hoặc đại diện địa chỉ số trong máy chủ. Tương tự, cổng có thể chứa tên cổng chuỗi hoặc số cổng dạng số For IPv6 addresses, >>> import socket >>> s1, s2 = socket.socketpair() >>> b1 = bytearray(b'----') >>> b2 = bytearray(b'0123456789') >>> b3 = bytearray(b'--------------') >>> s1.send(b'Mary had a little lamb') 22 >>> s2.recvmsg_into([b1, memoryview(b2)[2:9], b3]) (22, [], 0, None) >>> [b1, b2, b3] [bytearray(b'Mary'), bytearray(b'01 had a 9'), bytearray(b'little lamb---')]0 is appended to the host part if sockaddr contains meaningful scope_id. Thông thường điều này xảy ra đối với các địa chỉ multicast Để biết thêm thông tin về cờ, bạn có thể tham khảo getnameinfo(3) Nâng cao một >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]77 với đối số >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]78 không phải WASI ổ cắm. getprotobyname(tên giao thức)Dịch tên giao thức internet (ví dụ: >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]79) thành một hằng số phù hợp để chuyển làm đối số thứ ba (tùy chọn) cho hàm import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)8. Điều này thường chỉ cần thiết cho các ổ cắm được mở ở chế độ "thô" (); không phải WASI ổ cắm. getservbyname(tên dịch vụ[ , protocolname])Dịch tên dịch vụ internet và tên giao thức thành số cổng cho dịch vụ đó. Tên giao thức tùy chọn, nếu được cung cấp, phải là >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]82 hoặc >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]83, nếu không, bất kỳ giao thức nào cũng sẽ khớp Tăng một >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]84 với các đối số >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]85, >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]86 không phải WASI ổ cắm. getservbyport(cổng[ , protocolname])Dịch số cổng internet và tên giao thức thành tên dịch vụ cho dịch vụ đó. Tên giao thức tùy chọn, nếu được cung cấp, phải là >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]82 hoặc >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]83, nếu không, bất kỳ giao thức nào cũng sẽ khớp Tăng một >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]89 với các đối số >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]47, >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]86 không phải WASI ổ cắm. ntohl(x)Chuyển đổi số nguyên dương 32 bit từ mạng sang thứ tự byte lưu trữ. Trên các máy có thứ tự byte máy chủ giống với thứ tự byte mạng, đây là lệnh cấm; ổ cắm. ntohs(x)Chuyển đổi số nguyên dương 16 bit từ mạng sang thứ tự byte lưu trữ. Trên các máy có thứ tự byte máy chủ giống với thứ tự byte mạng, đây là lệnh cấm; Đã thay đổi trong phiên bản 3. 10. Tăng nếu x không vừa với số nguyên không dấu 16 bit. ổ cắm. htonl(x)Chuyển đổi số nguyên dương 32 bit từ máy chủ sang thứ tự byte mạng. Trên các máy có thứ tự byte máy chủ giống với thứ tự byte mạng, đây là lệnh cấm; ổ cắm. htons(x)Chuyển đổi số nguyên dương 16 bit từ máy chủ sang thứ tự byte mạng. Trên các máy có thứ tự byte máy chủ giống với thứ tự byte mạng, đây là lệnh cấm; Đã thay đổi trong phiên bản 3. 10. Tăng nếu x không vừa với số nguyên không dấu 16 bit. ổ cắm. inet_aton(ip_string)Chuyển đổi địa chỉ IPv4 từ định dạng chuỗi tứ giác chấm (ví dụ: '123. 45. 67. 89') sang định dạng nhị phân được đóng gói 32 bit, dưới dạng đối tượng byte có độ dài bốn ký tự. Điều này hữu ích khi trò chuyện với một chương trình sử dụng thư viện C tiêu chuẩn và cần các đối tượng thuộc loại >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]94, là loại C cho nhị phân được đóng gói 32 bit mà hàm này trả về cũng chấp nhận các chuỗi có ít hơn ba dấu chấm; Nếu chuỗi địa chỉ IPv4 được chuyển đến chức năng này không hợp lệ, sẽ tăng. Lưu ý rằng chính xác những gì hợp lệ phụ thuộc vào việc triển khai C cơ bản của >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]95 không hỗ trợ IPv6 và nên được sử dụng thay thế cho hỗ trợ ngăn xếp kép IPv4/v6 ổ cắm. inet_ntoa(packed_ip)Chuyển đổi địa chỉ IPv4 được đóng gói 32 bit (độ dài bốn byte) thành biểu diễn chuỗi bốn dấu chấm tiêu chuẩn của nó (ví dụ: '123. 45. 67. 89’). Điều này hữu ích khi trò chuyện với một chương trình sử dụng thư viện C tiêu chuẩn và cần các đối tượng thuộc loại >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]94, là loại C cho dữ liệu nhị phân được đóng gói 32 bit mà hàm này lấy làm đối số Nếu chuỗi byte được truyền cho hàm này không có độ dài chính xác là 4 byte, thì sẽ tăng lên. không hỗ trợ IPv6 và nên được sử dụng thay thế cho hỗ trợ ngăn xếp kép IPv4/v6 Changed in version 3. 5. Writable is now accepted. ổ cắm. inet_pton(address_family , ip_string)Chuyển đổi địa chỉ IP từ định dạng chuỗi dành riêng cho họ sang định dạng nhị phân được đóng gói. is useful when a library or network protocol calls for an object of type >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]94 (similar to ) or import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds)07 Các giá trị được hỗ trợ cho address_family hiện tại và. Nếu chuỗi địa chỉ IP ip_string không hợp lệ, sẽ tăng lên. Lưu ý rằng chính xác những gì hợp lệ phụ thuộc vào cả giá trị của address_family và triển khai cơ bản của >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]99 Hệ điều hành Unix, Windows Đã thay đổi trong phiên bản 3. 4. Đã thêm hỗ trợ Windows ổ cắm. inet_ntop(address_family , packed_ip)Chuyển đổi một địa chỉ IP được đóng gói (một số byte nhất định) thành biểu diễn chuỗi chuẩn, dành riêng cho họ (ví dụ: import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds)12 hoặc import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds)13). hữu ích khi một thư viện hoặc giao thức mạng trả về một đối tượng thuộc loại >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]94 (tương tự như ) hoặc import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds)07 Các giá trị được hỗ trợ cho address_family hiện tại và. Nếu đối tượng bytepack_ip không có độ dài chính xác cho họ địa chỉ đã chỉ định, sẽ tăng lên. được đưa ra cho các lỗi từ cuộc gọi đến Hệ điều hành Unix, Windows Đã thay đổi trong phiên bản 3. 4. Đã thêm hỗ trợ Windows Changed in version 3. 5. Writable is now accepted. ổ cắm. CMSG_LEN(độ dài)Trả về tổng độ dài, không có phần đệm ở cuối, của một mục dữ liệu phụ trợ với dữ liệu được liên kết có độ dài nhất định. Giá trị này thường có thể được sử dụng làm kích thước bộ đệm để nhận một mục dữ liệu phụ trợ, nhưng RFC 3542 yêu cầu sử dụng các ứng dụng di động và do đó bao gồm không gian cho phần đệm, ngay cả khi mục này sẽ là mục cuối cùng trong bộ đệm. Tăng nếu chiều dài nằm ngoài phạm vi giá trị cho phép Unix, không phải Emscripten, không phải WASI Hầu hết các nền tảng Unix Mới trong phiên bản 3. 3 ổ cắm. CMSG_SPACE(độ dài)Trả về kích thước bộ đệm cần thiết để nhận một mục dữ liệu phụ với dữ liệu được liên kết có độ dài nhất định, cùng với bất kỳ phần đệm theo sau nào. Không gian bộ đệm cần thiết để nhận nhiều mục là tổng các giá trị cho độ dài dữ liệu được liên kết của chúng. Tăng nếu chiều dài nằm ngoài phạm vi giá trị cho phép Lưu ý rằng một số hệ thống có thể hỗ trợ dữ liệu phụ trợ mà không cung cấp chức năng này. Cũng lưu ý rằng việc đặt kích thước bộ đệm bằng cách sử dụng kết quả của chức năng này có thể không giới hạn chính xác lượng dữ liệu phụ trợ có thể nhận được, vì dữ liệu bổ sung có thể vừa với vùng đệm Unix, không phải Emscripten, không phải WASI hầu hết các nền tảng Unix Mới trong phiên bản 3. 3 ổ cắm. getdefaulttimeout()Trả lại thời gian chờ mặc định tính bằng giây (float) cho các đối tượng ổ cắm mới. Giá trị của >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]30 chỉ ra rằng các đối tượng ổ cắm mới không có thời gian chờ. Khi mô-đun ổ cắm được nhập lần đầu tiên, giá trị mặc định là >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]30ổ cắm. setdefaulttimeout(thời gian chờ) Đặt thời gian chờ mặc định tính bằng giây (float) cho các đối tượng ổ cắm mới. Khi mô-đun ổ cắm được nhập lần đầu tiên, giá trị mặc định là >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]30. Xem các giá trị có thể và ý nghĩa tương ứng của chúngổ cắm. sethostname(tên) Đặt tên máy chủ của máy thành tên. Điều này sẽ tăng nếu bạn không có đủ quyền Đưa ra một import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds)34 với lập luận import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds)35 Unix Mới trong phiên bản 3. 3 ổ cắm. if_nameindex()Trả về danh sách các bộ thông tin giao diện mạng (chỉ số int, chuỗi tên). nếu cuộc gọi hệ thống không thành công Unix, Windows, không phải Emscripten, không phải WASI Mới trong phiên bản 3. 3 Đã thay đổi trong phiên bản 3. 8. Hỗ trợ Windows đã được thêm vào. Ghi chú Trên các giao diện mạng Windows có các tên khác nhau trong các ngữ cảnh khác nhau (tất cả các tên đều là ví dụ)
Hàm này trả về tên của biểu mẫu thứ hai từ danh sách, import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds)38 trong trường hợp ví dụ nàyổ cắm. if_nametoindex(if_name) Trả về số chỉ mục giao diện mạng tương ứng với tên giao diện. nếu không có giao diện với tên đã cho tồn tại Unix, Windows, không phải Emscripten, không phải WASI Mới trong phiên bản 3. 3 Đã thay đổi trong phiên bản 3. 8. Hỗ trợ Windows đã được thêm vào. See also “Tên giao diện” là tên được ghi trong tài liệu ổ cắm. if_indextoname(if_index)Trả về tên giao diện mạng tương ứng với số chỉ mục giao diện. nếu không có giao diện nào với chỉ mục đã cho tồn tại Unix, Windows, không phải Emscripten, không phải WASI Mới trong phiên bản 3. 3 Đã thay đổi trong phiên bản 3. 8. Hỗ trợ Windows đã được thêm vào. See also “Tên giao diện” là tên được ghi trong tài liệu ổ cắm. send_fds(sock , bộ đệm, fds[, flags[, address]])Gửi danh sách các bộ mô tả tệp fds qua một ổ cắm. Tham số fds là một chuỗi các bộ mô tả tệp. Tham khảo ý kiến của import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds)47 để biết tài liệu về các thông số này Unix, Windows, không phải Emscripten, không phải WASI Nền tảng Unix hỗ trợ và cơ chế import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds)49 Mới trong phiên bản 3. 9 ổ cắm. recv_fds(sock , bufsize, maxfds[, flags])Nhận tối đa bộ mô tả tệp maxfds từ một ổ cắm. Trả lại import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds)51. Tham khảo ý kiến của import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds)23 để biết tài liệu về các tham số này Unix, Windows, không phải Emscripten, không phải WASI Nền tảng Unix hỗ trợ và cơ chế import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds)49 Mới trong phiên bản 3. 9 Ghi chú Bất kỳ số nguyên bị cắt nào ở cuối danh sách bộ mô tả tệp Đối tượng ổ cắmCác đối tượng ổ cắm có các phương thức sau. Ngoại trừ , chúng tương ứng với các cuộc gọi hệ thống Unix áp dụng cho ổ cắm Đã thay đổi trong phiên bản 3. 2. Hỗ trợ cho giao thức đã được thêm vào. Thoát khỏi trình quản lý bối cảnh tương đương với việc gọi. ổ cắm. chấp nhận()Chấp nhận kết nối. Ổ cắm phải được liên kết với một địa chỉ và lắng nghe các kết nối. Giá trị trả về là một cặp import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds)57 trong đó conn là một đối tượng ổ cắm mới có thể sử dụng để gửi và nhận dữ liệu trên kết nối và địa chỉ là địa chỉ được liên kết với ổ cắm ở đầu kia của kết nối Ổ cắm mới được tạo là Đã thay đổi trong phiên bản 3. 4. Ổ cắm hiện không thể kế thừa. Đã thay đổi trong phiên bản 3. 5. Nếu lệnh gọi hệ thống bị gián đoạn và trình xử lý tín hiệu không đưa ra ngoại lệ, thì phương thức hiện sẽ thử lại lệnh gọi hệ thống thay vì đưa ra ngoại lệ (xem PEP 475 để biết lý do). ổ cắm. ràng buộc(địa chỉ)Liên kết ổ cắm với địa chỉ. Ổ cắm phải chưa bị ràng buộc. (Định dạng của địa chỉ phụ thuộc vào họ địa chỉ — xem ở trên. ) Tăng một import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds)59 với các đối số import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)91, import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds)61 không phải WASI ổ cắm. đóng()Đánh dấu ổ cắm đã đóng. Tài nguyên hệ thống cơ bản (e. g. một bộ mô tả tệp) cũng bị đóng khi tất cả các đối tượng tệp từ bị đóng. Khi điều đó xảy ra, tất cả các hoạt động trong tương lai trên đối tượng ổ cắm sẽ không thành công. Đầu từ xa sẽ không nhận thêm dữ liệu (sau khi xóa dữ liệu trong hàng đợi) Sockets are automatically closed when they are garbage-collected, but it is recommended to them explicitly, or to use a statement around them Đã thay đổi trong phiên bản 3. 6. hiện được nâng lên nếu xảy ra lỗi khi thực hiện lệnh gọi import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds)56 cơ bản. Ghi chú giải phóng tài nguyên được liên kết với kết nối nhưng không nhất thiết phải đóng kết nối ngay lập tức. Nếu bạn muốn đóng kết nối kịp thời, hãy gọi trước ổ cắm. kết nối(địa chỉ)Kết nối với ổ cắm từ xa tại địa chỉ. (Định dạng của địa chỉ phụ thuộc vào họ địa chỉ — xem ở trên. ) Nếu kết nối bị gián đoạn bởi tín hiệu, phương thức sẽ đợi cho đến khi kết nối hoàn tất hoặc tăng thời gian chờ, nếu trình xử lý tín hiệu không đưa ra ngoại lệ và ổ cắm đang chặn hoặc hết thời gian chờ. Đối với các ổ cắm không chặn, phương pháp này sẽ đưa ra một ngoại lệ nếu kết nối bị gián đoạn bởi một tín hiệu (hoặc ngoại lệ do bộ xử lý tín hiệu đưa ra) Đưa ra một import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds)72 với các đối số import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)91, import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds)61 Đã thay đổi trong phiên bản 3. 5. Phương thức hiện đợi cho đến khi kết nối hoàn tất thay vì đưa ra ngoại lệ nếu kết nối bị gián đoạn bởi tín hiệu, trình xử lý tín hiệu không đưa ra ngoại lệ và ổ cắm đang bị chặn hoặc hết thời gian chờ ( . không phải WASI ổ cắm. connect_ex(địa chỉ)Giống như import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds)76, nhưng trả về một chỉ báo lỗi thay vì đưa ra một ngoại lệ cho các lỗi được trả về bởi lệnh gọi import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds)77 cấp C (các sự cố khác, chẳng hạn như “không tìm thấy máy chủ”, vẫn có thể đưa ra các ngoại lệ). Chỉ báo lỗi là import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds)78 nếu thao tác thành công, nếu không thì giá trị của biến import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds)79. This is useful to support, for example, asynchronous connects Đưa ra một import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds)72 với các đối số import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)91, import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds)61 không phải WASI socket. detach()Put the socket object into closed state without actually closing the underlying file descriptor. The file descriptor is returned, and can be reused for other purposes Mới trong phiên bản 3. 2 socket. dup()Duplicate the socket Ổ cắm mới được tạo là Đã thay đổi trong phiên bản 3. 4. Ổ cắm hiện không thể kế thừa. không phải WASI socket. fileno()Return the socket’s file descriptor (a small integer), or -1 on failure. This is useful with Under Windows the small integer returned by this method cannot be used where a file descriptor can be used (such as ). Unix does not have this limitation socket. get_inheritable()Get the of the socket’s file descriptor or socket’s handle. >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]11 if the socket can be inherited in child processes, import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds)86 if it cannot Mới trong phiên bản 3. 4 socket. getpeername()Return the remote address to which the socket is connected. This is useful to find out the port number of a remote IPv4/v6 socket, for instance. (The format of the address returned depends on the address family — see above. ) Trên một số hệ thống, chức năng này không được hỗ trợ socket. getsockname()Return the socket’s own address. This is useful to find out the port number of an IPv4/v6 socket, for instance. (The format of the address returned depends on the address family — see above. ) socket. getsockopt(level , optname[ , buflen])Trả về giá trị của tùy chọn ổ cắm đã cho (xem trang hướng dẫn Unix getockopt(2)). The needed symbolic constants ( import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds)87 etc. ) are defined in this module. If buflen is absent, an integer option is assumed and its integer value is returned by the function. If buflen is present, it specifies the maximum length of the buffer used to receive the option in, and this buffer is returned as a bytes object. It is up to the caller to decode the contents of the buffer (see the optional built-in module for a way to decode C structures encoded as byte strings) không phải WASI socket. getblocking()Return >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]11 if socket is in blocking mode, import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds)86 if in non-blocking This is equivalent to checking import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds)91 Mới trong phiên bản 3. 7 socket. gettimeout()Return the timeout in seconds (float) associated with socket operations, or >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]30 if no timeout is set. This reflects the last call to or socket. ioctl(control , option) Platform Windows The method is a limited interface to the WSAIoctl system interface. Please refer to the Win32 documentation for more information On other platforms, the generic and functions may be used; they accept a socket object as their first argument Currently only the following control codes are supported. import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds)98, import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds)99, and import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)64 Đã thay đổi trong phiên bản 3. 6. ______0_______64 đã được thêm vào. socket. listen([backlog])Enable a server to accept connections. If backlog is specified, it must be at least 0 (if it is lower, it is set to 0); it specifies the number of unaccepted connections that the system will allow before refusing new connections. If not specified, a default reasonable value is chosen không phải WASI Changed in version 3. 5. The backlog parameter is now optional. Return a associated with the socket. The exact returned type depends on the arguments given to . These arguments are interpreted the same way as by the built-in function, except the only supported mode values are >>> import socket >>> s1, s2 = socket.socketpair() >>> b1 = bytearray(b'----') >>> b2 = bytearray(b'0123456789') >>> b3 = bytearray(b'--------------') >>> s1.send(b'Mary had a little lamb') 22 >>> s2.recvmsg_into([b1, memoryview(b2)[2:9], b3]) (22, [], 0, None) >>> [b1, b2, b3] [bytearray(b'Mary'), bytearray(b'01 had a 9'), bytearray(b'little lamb---')]04 (default), >>> import socket >>> s1, s2 = socket.socketpair() >>> b1 = bytearray(b'----') >>> b2 = bytearray(b'0123456789') >>> b3 = bytearray(b'--------------') >>> s1.send(b'Mary had a little lamb') 22 >>> s2.recvmsg_into([b1, memoryview(b2)[2:9], b3]) (22, [], 0, None) >>> [b1, b2, b3] [bytearray(b'Mary'), bytearray(b'01 had a 9'), bytearray(b'little lamb---')]05 and >>> import socket >>> s1, s2 = socket.socketpair() >>> b1 = bytearray(b'----') >>> b2 = bytearray(b'0123456789') >>> b3 = bytearray(b'--------------') >>> s1.send(b'Mary had a little lamb') 22 >>> s2.recvmsg_into([b1, memoryview(b2)[2:9], b3]) (22, [], 0, None) >>> [b1, b2, b3] [bytearray(b'Mary'), bytearray(b'01 had a 9'), bytearray(b'little lamb---')]06 The socket must be in blocking mode; it can have a timeout, but the file object’s internal buffer may end up in an inconsistent state if a timeout occurs Closing the file object returned by won’t close the original socket unless all other file objects have been closed and has been called on the socket object Ghi chú On Windows, the file-like object created by cannot be used where a file object with a file descriptor is expected, such as the stream arguments of socket. recv(bufsize[ , flags])Receive data from the socket. The return value is a bytes object representing the data received. The maximum amount of data to be received at once is specified by bufsize. See the Unix manual page recv(2) for the meaning of the optional argument flags; it defaults to zero Ghi chú For best match with hardware and network realities, the value of bufsize should be a relatively small power of 2, for example, 4096 Đã thay đổi trong phiên bản 3. 5. Nếu lệnh gọi hệ thống bị gián đoạn và trình xử lý tín hiệu không đưa ra ngoại lệ, thì phương thức hiện sẽ thử lại lệnh gọi hệ thống thay vì đưa ra ngoại lệ (xem PEP 475 để biết lý do). socket. recvfrom(bufsize[ , flags])Receive data from the socket. The return value is a pair >>> import socket >>> s1, s2 = socket.socketpair() >>> b1 = bytearray(b'----') >>> b2 = bytearray(b'0123456789') >>> b3 = bytearray(b'--------------') >>> s1.send(b'Mary had a little lamb') 22 >>> s2.recvmsg_into([b1, memoryview(b2)[2:9], b3]) (22, [], 0, None) >>> [b1, b2, b3] [bytearray(b'Mary'), bytearray(b'01 had a 9'), bytearray(b'little lamb---')]12 where bytes is a bytes object representing the data received and address is the address of the socket sending the data. See the Unix manual page recv(2) for the meaning of the optional argument flags; it defaults to zero. (The format of address depends on the address family — see above. ) Đã thay đổi trong phiên bản 3. 5. Nếu lệnh gọi hệ thống bị gián đoạn và trình xử lý tín hiệu không đưa ra ngoại lệ, thì phương thức hiện sẽ thử lại lệnh gọi hệ thống thay vì đưa ra ngoại lệ (xem PEP 475 để biết lý do). Changed in version 3. 7. For multicast IPv6 address, first item of address does not contain >>> import socket >>> s1, s2 = socket.socketpair() >>> b1 = bytearray(b'----') >>> b2 = bytearray(b'0123456789') >>> b3 = bytearray(b'--------------') >>> s1.send(b'Mary had a little lamb') 22 >>> s2.recvmsg_into([b1, memoryview(b2)[2:9], b3]) (22, [], 0, None) >>> [b1, b2, b3] [bytearray(b'Mary'), bytearray(b'01 had a 9'), bytearray(b'little lamb---')]0 part anymore. In order to get full IPv6 address use . socket. recvmsg(bufsize[ , ancbufsize[ , flags]]) Receive normal data (up to bufsize bytes) and ancillary data from the socket. Đối số ancbufsize đặt kích thước tính bằng byte của bộ đệm bên trong được sử dụng để nhận dữ liệu phụ trợ; . Appropriate buffer sizes for ancillary data can be calculated using or , and items which do not fit into the buffer might be truncated or discarded. The flags argument defaults to 0 and has the same meaning as for The return value is a 4-tuple. >>> import socket >>> s1, s2 = socket.socketpair() >>> b1 = bytearray(b'----') >>> b2 = bytearray(b'0123456789') >>> b3 = bytearray(b'--------------') >>> s1.send(b'Mary had a little lamb') 22 >>> s2.recvmsg_into([b1, memoryview(b2)[2:9], b3]) (22, [], 0, None) >>> [b1, b2, b3] [bytearray(b'Mary'), bytearray(b'01 had a 9'), bytearray(b'little lamb---')]19. The data item is a object holding the non-ancillary data received. The ancdata item is a list of zero or more tuples >>> import socket >>> s1, s2 = socket.socketpair() >>> b1 = bytearray(b'----') >>> b2 = bytearray(b'0123456789') >>> b3 = bytearray(b'--------------') >>> s1.send(b'Mary had a little lamb') 22 >>> s2.recvmsg_into([b1, memoryview(b2)[2:9], b3]) (22, [], 0, None) >>> [b1, b2, b3] [bytearray(b'Mary'), bytearray(b'01 had a 9'), bytearray(b'little lamb---')]21 representing the ancillary data (control messages) received. cmsg_level and cmsg_type are integers specifying the protocol level and protocol-specific type respectively, and cmsg_data is a object holding the associated data. The msg_flags item is the bitwise OR of various flags indicating conditions on the received message; see your system documentation for details. If the receiving socket is unconnected, address is the address of the sending socket, if available; otherwise, its value is unspecified On some systems, and can be used to pass file descriptors between processes over an socket. When this facility is used (it is often restricted to sockets), will return, in its ancillary data, items of the form >>> import socket >>> s1, s2 = socket.socketpair() >>> b1 = bytearray(b'----') >>> b2 = bytearray(b'0123456789') >>> b3 = bytearray(b'--------------') >>> s1.send(b'Mary had a little lamb') 22 >>> s2.recvmsg_into([b1, memoryview(b2)[2:9], b3]) (22, [], 0, None) >>> [b1, b2, b3] [bytearray(b'Mary'), bytearray(b'01 had a 9'), bytearray(b'little lamb---')]28, where fds is a object representing the new file descriptors as a binary array of the native C int type. Nếu phát sinh một ngoại lệ sau khi lệnh gọi hệ thống trả về, trước tiên, nó sẽ cố gắng đóng bất kỳ bộ mô tả tệp nào nhận được qua cơ chế này. Một số hệ thống không chỉ ra độ dài cắt ngắn của các mục dữ liệu phụ trợ chỉ được nhận một phần. Nếu một mục dường như mở rộng ra ngoài phần cuối của bộ đệm, sẽ phát hành một và sẽ trả lại phần của nó nằm trong bộ đệm với điều kiện là nó không bị cắt bớt trước khi bắt đầu dữ liệu liên quan của nó Trên các hệ thống hỗ trợ cơ chế import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds)49, hàm sau đây sẽ nhận tối đa bộ mô tả tệp maxfds, trả về dữ liệu thông báo và danh sách chứa bộ mô tả (trong khi bỏ qua các điều kiện không mong muốn chẳng hạn như nhận được thông báo điều khiển không liên quan). Xem thêm import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds) Unix Hầu hết các nền tảng Unix Mới trong phiên bản 3. 3 Đã thay đổi trong phiên bản 3. 5. Nếu lệnh gọi hệ thống bị gián đoạn và trình xử lý tín hiệu không đưa ra ngoại lệ, thì phương thức hiện sẽ thử lại lệnh gọi hệ thống thay vì đưa ra ngoại lệ (xem PEP 475 để biết lý do). ổ cắm. recvmsg_into(buffers[ , ancbufsize[ , flags]])Receive normal data and ancillary data from the socket, behaving as would, but scatter the non-ancillary data into a series of buffers instead of returning a new bytes object. Đối số bộ đệm phải là đối tượng có thể lặp lại của các đối tượng xuất bộ đệm có thể ghi (e. g. các đối tượng); . Hệ điều hành có thể đặt giới hạn (giá trị >>> import socket >>> s1, s2 = socket.socketpair() >>> b1 = bytearray(b'----') >>> b2 = bytearray(b'0123456789') >>> b3 = bytearray(b'--------------') >>> s1.send(b'Mary had a little lamb') 22 >>> s2.recvmsg_into([b1, memoryview(b2)[2:9], b3]) (22, [], 0, None) >>> [b1, b2, b3] [bytearray(b'Mary'), bytearray(b'01 had a 9'), bytearray(b'little lamb---')]39) về số lượng bộ đệm có thể được sử dụng. The ancbufsize and flags arguments have the same meaning as for The return value is a 4-tuple. >>> import socket >>> s1, s2 = socket.socketpair() >>> b1 = bytearray(b'----') >>> b2 = bytearray(b'0123456789') >>> b3 = bytearray(b'--------------') >>> s1.send(b'Mary had a little lamb') 22 >>> s2.recvmsg_into([b1, memoryview(b2)[2:9], b3]) (22, [], 0, None) >>> [b1, b2, b3] [bytearray(b'Mary'), bytearray(b'01 had a 9'), bytearray(b'little lamb---')]41, where nbytes is the total number of bytes of non-ancillary data written into the buffers, and ancdata, msg_flags and address are the same as for Example >>> import socket >>> s1, s2 = socket.socketpair() >>> b1 = bytearray(b'----') >>> b2 = bytearray(b'0123456789') >>> b3 = bytearray(b'--------------') >>> s1.send(b'Mary had a little lamb') 22 >>> s2.recvmsg_into([b1, memoryview(b2)[2:9], b3]) (22, [], 0, None) >>> [b1, b2, b3] [bytearray(b'Mary'), bytearray(b'01 had a 9'), bytearray(b'little lamb---')] Unix Hầu hết các nền tảng Unix Mới trong phiên bản 3. 3 socket. recvfrom_into(buffer[ , nbytes[ , flags]])Receive data from the socket, writing it into buffer instead of creating a new bytestring. The return value is a pair >>> import socket >>> s1, s2 = socket.socketpair() >>> b1 = bytearray(b'----') >>> b2 = bytearray(b'0123456789') >>> b3 = bytearray(b'--------------') >>> s1.send(b'Mary had a little lamb') 22 >>> s2.recvmsg_into([b1, memoryview(b2)[2:9], b3]) (22, [], 0, None) >>> [b1, b2, b3] [bytearray(b'Mary'), bytearray(b'01 had a 9'), bytearray(b'little lamb---')]43 where nbytes is the number of bytes received and address is the address of the socket sending the data. See the Unix manual page recv(2) for the meaning of the optional argument flags; it defaults to zero. (The format of address depends on the address family — see above. )socket. recv_into(buffer[ , nbytes[ , flags]]) Receive up to nbytes bytes from the socket, storing the data into a buffer rather than creating a new bytestring. If nbytes is not specified (or 0), receive up to the size available in the given buffer. Returns the number of bytes received. See the Unix manual page recv(2) for the meaning of the optional argument flags; it defaults to zero socket. send(bytes[ , flags])Send data to the socket. The socket must be connected to a remote socket. The optional flags argument has the same meaning as for above. Returns the number of bytes sent. Applications are responsible for checking that all data has been sent; if only some of the data was transmitted, the application needs to attempt delivery of the remaining data. For further information on this topic, consult the Đã thay đổi trong phiên bản 3. 5. Nếu lệnh gọi hệ thống bị gián đoạn và trình xử lý tín hiệu không đưa ra ngoại lệ, thì phương thức hiện sẽ thử lại lệnh gọi hệ thống thay vì đưa ra ngoại lệ (xem PEP 475 để biết lý do). socket. sendall(bytes[ , flags])Send data to the socket. The socket must be connected to a remote socket. The optional flags argument has the same meaning as for above. Unlike , this method continues to send data from bytes until either all data has been sent or an error occurs. >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]30 is returned on success. On error, an exception is raised, and there is no way to determine how much data, if any, was successfully sent Changed in version 3. 5. The socket timeout is no more reset each time data is sent successfully. The socket timeout is now the maximum total duration to send all data. Đã thay đổi trong phiên bản 3. 5. Nếu lệnh gọi hệ thống bị gián đoạn và trình xử lý tín hiệu không đưa ra ngoại lệ, thì phương thức hiện sẽ thử lại lệnh gọi hệ thống thay vì đưa ra ngoại lệ (xem PEP 475 để biết lý do). socket. sendto(bytes , address)socket. sendto(bytes , flags , address)Send data to the socket. The socket should not be connected to a remote socket, since the destination socket is specified by address. The optional flags argument has the same meaning as for above. Return the number of bytes sent. (The format of address depends on the address family — see above. ) Raises an >>> import socket >>> s1, s2 = socket.socketpair() >>> b1 = bytearray(b'----') >>> b2 = bytearray(b'0123456789') >>> b3 = bytearray(b'--------------') >>> s1.send(b'Mary had a little lamb') 22 >>> s2.recvmsg_into([b1, memoryview(b2)[2:9], b3]) (22, [], 0, None) >>> [b1, b2, b3] [bytearray(b'Mary'), bytearray(b'01 had a 9'), bytearray(b'little lamb---')]51 with arguments import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)91, import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds)61 Đã thay đổi trong phiên bản 3. 5. Nếu lệnh gọi hệ thống bị gián đoạn và trình xử lý tín hiệu không đưa ra ngoại lệ, thì phương thức hiện sẽ thử lại lệnh gọi hệ thống thay vì đưa ra ngoại lệ (xem PEP 475 để biết lý do). socket. sendmsg(buffers[ , ancdata[ , flags[ , address]]])Gửi dữ liệu thông thường và phụ trợ đến ổ cắm, thu thập dữ liệu không phụ trợ từ một loạt bộ đệm và nối nó thành một thông báo duy nhất. The buffers argument specifies the non-ancillary data as an iterable of (e. g. objects); the operating system may set a limit ( value >>> import socket >>> s1, s2 = socket.socketpair() >>> b1 = bytearray(b'----') >>> b2 = bytearray(b'0123456789') >>> b3 = bytearray(b'--------------') >>> s1.send(b'Mary had a little lamb') 22 >>> s2.recvmsg_into([b1, memoryview(b2)[2:9], b3]) (22, [], 0, None) >>> [b1, b2, b3] [bytearray(b'Mary'), bytearray(b'01 had a 9'), bytearray(b'little lamb---')]39) on the number of buffers that can be used. The ancdata argument specifies the ancillary data (control messages) as an iterable of zero or more tuples >>> import socket >>> s1, s2 = socket.socketpair() >>> b1 = bytearray(b'----') >>> b2 = bytearray(b'0123456789') >>> b3 = bytearray(b'--------------') >>> s1.send(b'Mary had a little lamb') 22 >>> s2.recvmsg_into([b1, memoryview(b2)[2:9], b3]) (22, [], 0, None) >>> [b1, b2, b3] [bytearray(b'Mary'), bytearray(b'01 had a 9'), bytearray(b'little lamb---')]21, where cmsg_level and cmsg_type are integers specifying the protocol level and protocol-specific type respectively, and cmsg_data is a bytes-like object holding the associated data. Note that some systems (in particular, systems without ) might support sending only one control message per call. The flags argument defaults to 0 and has the same meaning as for . If address is supplied and not >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]30, it sets a destination address for the message. The return value is the number of bytes of non-ancillary data sent The following function sends the list of file descriptors fds over an socket, on systems which support the import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds)49 mechanism. See also import socket, array def send_fds(sock, msg, fds): return sock.sendmsg([msg], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", fds))]) Unix, not WASI Hầu hết các nền tảng Unix Raises an >>> import socket >>> s1, s2 = socket.socketpair() >>> b1 = bytearray(b'----') >>> b2 = bytearray(b'0123456789') >>> b3 = bytearray(b'--------------') >>> s1.send(b'Mary had a little lamb') 22 >>> s2.recvmsg_into([b1, memoryview(b2)[2:9], b3]) (22, [], 0, None) >>> [b1, b2, b3] [bytearray(b'Mary'), bytearray(b'01 had a 9'), bytearray(b'little lamb---')]65 with arguments import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)91, import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds)61 Mới trong phiên bản 3. 3 Đã thay đổi trong phiên bản 3. 5. Nếu lệnh gọi hệ thống bị gián đoạn và trình xử lý tín hiệu không đưa ra ngoại lệ, thì phương thức hiện sẽ thử lại lệnh gọi hệ thống thay vì đưa ra ngoại lệ (xem PEP 475 để biết lý do). socket. sendmsg_afalg([msg , ]* , op[ , iv[ , assoclen[ , flags]]])Specialized version of for socket. Set mode, IV, AEAD associated data length and flags for socket Linux >= 2. 6. 38 Mới trong phiên bản 3. 6 socket. sendfile(file , offset=0 , count=None)Send a file until EOF is reached by using high-performance and return the total number of bytes which were sent. file must be a regular file object opened in binary mode. If is not available (e. g. Windows) or file is not a regular file will be used instead. offset tells from where to start reading the file. If specified, count is the total number of bytes to transmit as opposed to sending the file until EOF is reached. File position is updated on return or also in case of error in which case can be used to figure out the number of bytes which were sent. The socket must be of type. Non-blocking sockets are not supported Mới trong phiên bản 3. 5 socket. set_inheritable(inheritable)Set the of the socket’s file descriptor or socket’s handle Mới trong phiên bản 3. 4 socket. setblocking(flag)Set blocking or non-blocking mode of the socket. if flag is false, the socket is set to non-blocking, else to blocking mode This method is a shorthand for certain calls
Changed in version 3. 7. The method no longer applies flag on . socket. settimeout(value)Set a timeout on blocking socket operations. The value argument can be a nonnegative floating point number expressing seconds, or >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]30. If a non-zero value is given, subsequent socket operations will raise a exception if the timeout period value has elapsed before the operation has completed. If zero is given, the socket is put in non-blocking mode. If >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]30 is given, the socket is put in blocking mode For further information, please consult the Changed in version 3. 7. The method no longer toggles flag on . socket. setsockopt(level , optname , value. )socket. setsockopt(level , optname , value. buffer)socket. setsockopt(level , optname , None , optlen. int)Set the value of the given socket option (see the Unix manual page setsockopt(2)). The needed symbolic constants are defined in the module ( import socket, array def recv_fds(sock, msglen, maxfds): fds = array.array("i") # Array of ints msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: # Append data, ignoring any truncated integers at the end. fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return msg, list(fds)87 etc. ). The value can be an integer, >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]30 or a representing a buffer. In the later case it is up to the caller to ensure that the bytestring contains the proper bits (see the optional built-in module for a way to encode C structures as bytestrings). Khi giá trị được đặt thành >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]30, đối số optlen là bắt buộc. It’s equivalent to call import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)44 C function with >>> import socket >>> s1, s2 = socket.socketpair() >>> b1 = bytearray(b'----') >>> b2 = bytearray(b'0123456789') >>> b3 = bytearray(b'--------------') >>> s1.send(b'Mary had a little lamb') 22 >>> s2.recvmsg_into([b1, memoryview(b2)[2:9], b3]) (22, [], 0, None) >>> [b1, b2, b3] [bytearray(b'Mary'), bytearray(b'01 had a 9'), bytearray(b'little lamb---')]95 and >>> import socket >>> s1, s2 = socket.socketpair() >>> b1 = bytearray(b'----') >>> b2 = bytearray(b'0123456789') >>> b3 = bytearray(b'--------------') >>> s1.send(b'Mary had a little lamb') 22 >>> s2.recvmsg_into([b1, memoryview(b2)[2:9], b3]) (22, [], 0, None) >>> [b1, b2, b3] [bytearray(b'Mary'), bytearray(b'01 had a 9'), bytearray(b'little lamb---')]96 Changed in version 3. 5. Writable is now accepted. Changed in version 3. 6. setsockopt(level, optname, None, optlen. int) form added. không phải WASI ổ cắm. tắt(cách thức)Shut down one or both halves of the connection. If how is >>> import socket >>> s1, s2 = socket.socketpair() >>> b1 = bytearray(b'----') >>> b2 = bytearray(b'0123456789') >>> b3 = bytearray(b'--------------') >>> s1.send(b'Mary had a little lamb') 22 >>> s2.recvmsg_into([b1, memoryview(b2)[2:9], b3]) (22, [], 0, None) >>> [b1, b2, b3] [bytearray(b'Mary'), bytearray(b'01 had a 9'), bytearray(b'little lamb---')]97, further receives are disallowed. If how is >>> import socket >>> s1, s2 = socket.socketpair() >>> b1 = bytearray(b'----') >>> b2 = bytearray(b'0123456789') >>> b3 = bytearray(b'--------------') >>> s1.send(b'Mary had a little lamb') 22 >>> s2.recvmsg_into([b1, memoryview(b2)[2:9], b3]) (22, [], 0, None) >>> [b1, b2, b3] [bytearray(b'Mary'), bytearray(b'01 had a 9'), bytearray(b'little lamb---')]98, further sends are disallowed. If how is >>> import socket >>> s1, s2 = socket.socketpair() >>> b1 = bytearray(b'----') >>> b2 = bytearray(b'0123456789') >>> b3 = bytearray(b'--------------') >>> s1.send(b'Mary had a little lamb') 22 >>> s2.recvmsg_into([b1, memoryview(b2)[2:9], b3]) (22, [], 0, None) >>> [b1, b2, b3] [bytearray(b'Mary'), bytearray(b'01 had a 9'), bytearray(b'little lamb---')]99, further sends and receives are disallowed không phải WASI socket. share(process_id)Duplicate a socket and prepare it for sharing with a target process. The target process must be provided with process_id. The resulting bytes object can then be passed to the target process using some form of interprocess communication and the socket can be recreated there using . Once this method has been called, it is safe to close the socket since the operating system has already duplicated it for the target process các cửa sổ Mới trong phiên bản 3. 3 Note that there are no methods import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)9 or >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]0; use and without flags argument instead Socket objects also have these (read-only) attributes that correspond to the values given to the constructor socket. familyThe socket family socket. typeThe socket type socket. protoThe socket protocol Notes on socket timeoutsA socket object can be in one of three modes. chặn, không chặn hoặc hết thời gian chờ. Sockets are by default always created in blocking mode, but this can be changed by calling
Ghi chú At the operating system level, sockets in timeout mode are internally set in non-blocking mode. Also, the blocking and timeout modes are shared between file descriptors and socket objects that refer to the same network endpoint. This implementation detail can have visible consequences if e. g. you decide to use the of a socket Timeouts and the import socket, array def send_fds(sock, msg, fds): return sock.sendmsg([msg], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", fds))]) 10 methodThe operation is also subject to the timeout setting, and in general it is recommended to call before calling or pass a timeout parameter to . However, the system network stack may also return a connection timeout error of its own regardless of any Python socket timeout setting Timeouts and the import socket, array def send_fds(sock, msg, fds): return sock.sendmsg([msg], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", fds))]) 15 methodIf is not , sockets returned by the method inherit that timeout. Otherwise, the behaviour depends on settings of the listening socket
ExampleHere are four minimal example programs using the TCP/IP protocol. a server that echoes all data that it receives back (servicing only one client), and a client using it. Note that a server must perform the sequence import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)8, , , (possibly repeating the to service more than one client), while a client only needs the sequence import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)8, . Also note that the server does not / on the socket it is listening on but on the new socket returned by The first two examples support IPv4 only # Echo server program import socket HOST = '' # Symbolic name meaning all available interfaces PORT = 50007 # Arbitrary non-privileged port with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.bind((HOST, PORT)) s.listen(1) conn, addr = s.accept() with conn: print('Connected by', addr) while True: data = conn.recv(1024) if not data: break conn.sendall(data) # Echo client program import socket HOST = 'daring.cwi.nl' # The remote host PORT = 50007 # The same port as used by the server with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect((HOST, PORT)) s.sendall(b'Hello, world') data = s.recv(1024) print('Received', repr(data)) The next two examples are identical to the above two, but support both IPv4 and IPv6. The server side will listen to the first address family available (it should listen to both instead). On most of IPv6-ready systems, IPv6 will take precedence and the server may not accept IPv4 traffic. The client side will try to connect to the all addresses returned as a result of the name resolution, and sends traffic to the first one connected successfully # Echo server program import socket import sys HOST = None # Symbolic name meaning all available interfaces PORT = 50007 # Arbitrary non-privileged port s = None for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE): af, socktype, proto, canonname, sa = res try: s = socket.socket(af, socktype, proto) except OSError as msg: s = None continue try: s.bind(sa) s.listen(1) except OSError as msg: s.close() s = None continue break if s is None: print('could not open socket') sys.exit(1) conn, addr = s.accept() with conn: print('Connected by', addr) while True: data = conn.recv(1024) if not data: break conn.send(data) # Echo client program import socket import sys HOST = 'daring.cwi.nl' # The remote host PORT = 50007 # The same port as used by the server s = None for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM): af, socktype, proto, canonname, sa = res try: s = socket.socket(af, socktype, proto) except OSError as msg: s = None continue try: s.connect(sa) except OSError as msg: s.close() s = None continue break if s is None: print('could not open socket') sys.exit(1) with s: s.sendall(b'Hello, world') data = s.recv(1024) print('Received', repr(data)) The next example shows how to write a very simple network sniffer with raw sockets on Windows. The example requires administrator privileges to modify the interface import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)0 The next example shows how to use the socket interface to communicate to a CAN network using the raw socket protocol. To use CAN with the broadcast manager protocol instead, open a socket with import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)1 After binding ( import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)83) or connecting () the socket, you can use the , and the operations (and their counterparts) on the socket object as usual This last example might require special privileges import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)2 Running an example several times with too small delay between executions, could lead to this error import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)3 This is because the previous execution has left the socket in a import socket, array def send_fds(sock, msg, fds): return sock.sendmsg([msg], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", fds))])35 state, and can’t be immediately reused There is a flag to set, in order to prevent this, import socket, array def send_fds(sock, msg, fds): return sock.sendmsg([msg], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", fds))])37 import socket addr = ("", 8080) # all interfaces, port 8080 if socket.has_dualstack_ipv6(): s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True) else: s = socket.create_server(addr)4 the >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', 80))]21 flag tells the kernel to reuse a local socket in import socket, array def send_fds(sock, msg, fds): return sock.sendmsg([msg], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", fds))])35 state, without waiting for its natural timeout to expire See also For an introduction to socket programming (in C), see the following papers
both in the UNIX Programmer’s Manual, Supplementary Documents 1 (sections PS1. 7 and PS1. 8). The platform-specific reference material for the various socket-related system calls are also a valuable source of information on the details of socket semantics. For Unix, refer to the manual pages; for Windows, see the WinSock (or Winsock 2) specification. For IPv6-ready APIs, readers may want to refer to RFC 3493 titled Basic Socket Interface Extensions for IPv6 How to get client IP address in socket programming Python?Algorithm . Import the socket module Get the hostname using the socket. gethostname() method and store it in a variable Find the IP address by passing the hostname as an argument to the socket. gethostbyname() method and store it in a variable Python ổ cắm thô là gì?Raw socket is a layer 2 python library for communication using the MAC addresses only . Điều này cho phép bạn tạo một hệ thống liên lạc Ethernet/WiFi tùy chỉnh không sử dụng IP cũng như TCP/UDP hoặc để gỡ lỗi các khung tùy chỉnh như SERCOS III, Profibus, ARP, PTP,. Các phiên bản Python đã thử nghiệm. 2. 7.
Gói IP thô là gì?The Raw IP interface lets a client program send and receive arbitrary IP packets on any IP protocol except TCP and UDP . Chỉ một khách hàng có thể sử dụng bất kỳ giao thức cụ thể nào tại một thời điểm. Chỉ những khách hàng trong danh sách tuân thủ mới có thể sử dụng giao diện IP thô.
Làm cách nào để lấy IP từ FQDN bằng Python?Bạn có thể sử dụng ổ cắm. getaddrinfo . Điều đó sẽ cung cấp cho bạn địa chỉ IP khác nhau được liên kết với tên và cũng có thể cung cấp cho bạn địa chỉ IPv6. |