Python network programming (i)

Source: Internet
Author: User

Introduction to Sockets 1. There are many ways in which local interprocess communication (IPC), such as
    • Queue
    • Synchronization (mutex, condition variable, etc.)

The above means of communication is the way of communication between different processes on a single machine, so the problem is

How does the process communicate between processes in the network?

2. How processes communicate across the network

The first problem is how to uniquely identify a process, or the communication is out of the question!

A process can be uniquely identified locally through the process PID, but this is not feasible in the network.

In fact, the TCP/IP protocol family has helped us solve this problem, the network layer "IP address" can uniquely identify the host in the network, and the Transport Layer "protocol + port" can uniquely identify the host application (process).

This makes it possible to identify the process of the ip地址,协议,端口 network, and the process communication in the network can use this flag to interact with other processes.

3. What is a socket

A socket (short name 套接字 ) is a way of interprocess communication, and one of the major differences between it and other interprocess communication is:

It can realize inter-process communication between different hosts, and most of the various services on our network are based on sockets to complete communication.

For example, we browse the Web every day, QQ chat, send and receive email, etc.

4. Create a socket

Using the socket module function in Python, the socket can be completed:

Socket.socket (AddressFamily, Type)
Description

The function Socket.socket creates a socket that returns the descriptor of the socket with two parameters:

    • Address Family: You can choose af_inet (for inter-Internet interprocess communication) or Af_unix (for the same machine interprocess communication), which is commonly used in the actual work af_inet
    • Type: Socket type, either sock_stream (stream socket, primarily for TCP protocol) or SOCK_DGRAM (datagram socket, primarily for UDP protocol)

Create a TCP socket (TCP socket)

Import= socket.socket (socket.af_inet, socket. SOCK_STREAM)print'Socket Created'

Create a UDP socket (UDP socket)

Import= socket.socket (socket.af_inet, socket. SOCK_DGRAM)print'Socket Created'

Python network programming (i)

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.