Python Network Programming Basics

Source: Internet
Author: User

  The socket (socket) starts with UNIX, which is what people call BSD Unix. There are two types of socket families: file-system-based and network-based. The first is based on the file system, the address family is expressed as: Af_unix (or af_local), the second is a network socket, is network-based, the address family is represented as Af_inet (Af_inet6 represents IPv6). Added support for a Linux socket in Python 2.5: af_netlink (no connection [see below]) The socket family allows the IPC between user code and kernel code to use the standard BSD socket interface. Python only supports Af_unix,af_netlink, and the Af_inet family. The most widely used one is described here: af_inet.

Depending on the type of socket, it can be divided into connection-oriented and non-connected.

Connection-oriented, a connection needs to be established before communication, which provides sequential, reliable, and not repetitive data transfer, and is not added to the boundary. The primary protocol for implementing this connection is the Transmission Control Protocol (TCP), which corresponds to a socket type of sock_stream. Sockets use Internet Protocol (IP) to locate hosts in the network, the TCP/IP protocol, to support connection-oriented sockets.

No connection, no need to establish a connection to communicate. The primary protocol that implements this connection is the User Datagram Protocol (that is, UDP), which specifies that the socket type is SOCK_DGRAM. Sockets use the Internet Protocol to find hosts in the network, the UDP/IP protocol, to support non-connected sockets.

  1. Socket () module function

 The syntax for creating a socket () socket is as follows:

Sockets (socket_family, Socket_type, protocol=0)

Socket_family generally Af_unix or af_inet,socket_type can be sock_stream or sock_dgram,protocol generally not filled, the default is 0.

Because there are too many properties in the socket module, the "from socket Import *" is used here to reduce the code length.

Create a socket for TCP/IP:

Tcpsocket = socket (af_inet, SOCK_STREAM)

Similarly, create a UDP/IP socket:

Udpsocket = socket (af_inet, SOCK_STREAM)

  

2. Socket object (built-in) method

The common socket object functions in Python are as follows:

 3. The C/S model based on TCP

Python Network Programming Basics

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

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

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