Socket communication in Python

Source: Internet
Author: User

The concept of a protocol

different countriessay differentof theLanguage PersonDoor toCommunicationyou need to use English ., as long as there is an agreement that everyone can agree on,this way inComputeron tocompliance with the network communication protocol is calledTCP/IP protocolThe early computer network, all by the manufacturers themselves set a set of agreements,Ibm,Appleand theMicrosofthave their own network protocols, incompatible with each other. In order to connect all the different types of computers in the world, it is necessary to stipulate a set of universal protocols, in order to achieve the goal of Internet, Internet Protocol cluster (Internet Protocol Suite) is the Universal protocol standard. Because the Internet Protocol contains hundreds of protocol standards, the two most important protocols areTCPand theIPagreement, so everyone put the Internet protocol for shortTCP/IPProtocol

The four most common protocols

Application layer , Transport layer , network layer , link layer

differentiated into 7 layers is the

Layer, layer, Transport layer , network layer, Data link layers , physical layer Application

TCP/IP is a protocol group not two tiers

The concept of ports

in a Linux system, the port can have 65536(2 of the Time Square) of the many!

Since there are so many, the operating system for unified management, so the number, this is the port number

ports are marked by port number with only integers, ranging from 0 to 65535

Ports (well-known ports and dynamic ports)

well-known ports Known port number, ranging from 0 to 1023 like Some of the common features used by the number is fixed

The range of dynamic ports is from 1024 to 65535 This is a general random assignment that is not fixed

View Port Netstat-an

It is generally done by IP address + port number to complete different services.

IP Address

A string of numbers used to mark a computer in the network, such as 192.168.1.1; is unique on the local LAN.

each one IP address includes two parts: network address and host address

Class A IP address

a The class A IP address consists of a 1 - byte network address and a 3 -byte host address, and the highest bit of the network address must be "0" ,

address range 1.0.0.1-126.255.255.254

Binary representation is:00000001 00000000 00000000 00000001-01111110 11111111 11111111 11111110

available for Class A network has 126 , each network can accommodate 1677214 hosts

Class B IP Address

a The class B IP address consists of a 2 - byte network address and a 2 - byte host address, and the highest bit of the network address must be "10 ",

address range 128.1.0.1-191.255.255.254

Binary representation is:10000000 00000001 00000000 00000001-10111111 11111111 11111111 11111110

available for class B Network has 16384 , each network can accommodate 65534 host

Class C IP address

a The class C IP address consists of a 3 - byte network address and a 1 - byte host address, and the highest bit of the network address must be " the"

Range 192.0.1.1-223.255.255.254

Binary representation is : 11000000 00000000 00000001 00000001-11011111 11111111 11111110 11111110

class C network of up to 2097152 , each network can accommodate 254 hosts

Class D addresses for multipoint broadcasts

class D IP address the first byte begins with "1110" , which is a specially reserved address.

it does not point to a particular network, and currently this type of address is used in multipoint broadcasts ( Multicast) in

Multicast addresses are used to address a group of computers at one time.

address range 224.0.0.1-239.255.255.254

class IP address

start with "1111" and use retention for the future

class E address reserved for experimentation and development purposes only

Private IP

in so many networks IP , a part of the international IP address is used for our LAN, but also

it belongs to the private network . IP, not used in the public network, their scope is:

10.0.0.0~10.255.255.255

172.16.0.0~172.31.255.255

192.168.0.0~192.168.255.255

Note

IP Address 127. 0. 0. 1~127. 255. 255. 255 for loop testing,

such as:127.0.0.1 can represent the native IP address, http://127.0.0.1 can be used to test the local configuration of the Web server.

Subnet Mask

function:is to put aIpThe address is divided into network address and host address two The molecular network mask setting must follow certain rules. With theIPaddress is the same, the length of the subnet mask is also +bit, the left is the network bit, with binary digits"1"to the right is the host bit, with a binary number"0"representation. AssumptionsIPaddress is"192.168.1.1"The subnet mask is"255.255.255.0". among them,"1"have a -representing the correspondingIPAddress left -bit is the network number;"0"have a8representing the correspondingIPAddress Right8bit is the host number. In this way, the subnet mask determines aIPAddress of +bit binary number which is the network number, which is the host number. This is used forTCP/IPthe network of the Protocol is very important, only through the subnet mask, to indicate that a host is located in the subnet and other subnets, so that the network works

Socket (SOCKET)

Communication between processes in the network

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 " uniquely identifies the application (process) in the host. using IP address, protocol, port can identify the process of the network, process communication in the network can use this flag to interact with other processes , to achieve process communication between different hosts

Create a socket

Socket.socket (AddressFamily, Type)

two parameters Address Family: Can choose af_inet (for inter-Internet interprocess communication) or Af_unix (for the same machine interprocess communication), the actual work of the af_inet is actually the network properties of TCP/IP

Type: Socket type, either sock_stream (stream socket, primarily for TCP protocol) or SOCK_DGRAM (datagram socket, primarily for UDP protocol)

Two common sockets

TCP Transmission Slow but stable

UDP transmission fast, but easy to drop packets

Socket communication in Python

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.