Python server development 2: Python network Basics

Source: Internet
Author: User
Tags to domain

The network is divided from bottom up into the physical layer, data link layer, network layer, transmission layer, Session Layer, presentation layer and application layer.

HTTP is a high-level protocol, while TCP/IP is a protocol set that contains many sub-protocols. Including FTP, UDP, TCP, and IP at the transport layer, as well as HTTP and telnet. HTTP is a sub-Protocol of TCP/IP.

Socket is the encapsulation and Application of TCP/IP protocol (programmer level ). It can also be said that the TPC/IP protocol is the transport layer protocol, mainly to solve how data is transmitted over the network, while HTTP is the application layer protocol, mainly to solve how to package data.

When transmitting data, we can only use the (Transport Layer) TCP/IP protocol. However, without the application layer, we cannot identify the data content. If we want to make the transmitted data meaningful, you must use the application layer protocol. There are many application layer protocols, such as HTTP, FTP, and TELNET. You can also define the application layer protocol by yourself. The WEB uses HTTP as the application layer protocol to encapsulate HTTP text information, and then uses TCP/IP as the transport layer protocol to send it to the network.

What is the most commonly used socket? In fact, socket is the encapsulation of the TCP/IP protocol. Socket itself is not a protocol, but an interface (API) called through Socket, we can use the TCP/IP protocol. In fact, the Socket and TCP/IP protocols are not necessarily related. The Socket programming interface is designed to adapt to other network protocols. Therefore, the appearance of Socket only makes it easier for programmers to use the TCP/IP protocol stack. It is an abstraction of the TCP/IP protocol, this forms some of the most basic function interfaces we know, such as create, listen, connect, accept, send, read, and write.

TCP/IP is just a protocol stack. Like the operating mechanism of the operating system, it must be implemented in detail and provide external operation interfaces. Just as the operating system provides standard programming interfaces, such as win32 programming interfaces, TCP/IP also provides interfaces that programmers can use for network development. This is the Socket programming interface.

There is a more vivid description: HTTP is a car that provides a specific form of encapsulation or display data; Socket is an engine that provides network communication capabilities.

In fact, the TCP at the transport layer is based on the IP protocol at the network layer, while the HTTP protocol at the application layer is based on the TCP protocol at the transport layer, and the Socket itself is not a protocol, as mentioned above, it only provides an interface for TCP or UDP programming.

 

To establish a network connection using a Socket:

To establish a Socket connection, you must have at least one Socket. One of them runs on the client, which is called ClientSocket, And the other runs on the server, which is called ServerSocket.

The connection process between sockets is divided into three steps: server listening, client requests, and connection confirmation.

1. Server listening: the server socket does not locate the specific client socket, but is waiting for connection. It monitors the network in real time and waits for client connection requests.

2. Client request: the client socket initiates a connection request, and the target is the server socket. Therefore, the client socket must first describe the socket of the server to be connected, point out the address and port number of the socket on the server, and then submit a connection request to the socket on the server.

3. Connection Confirmation: when the server socket monitors or receives a connection request from the client socket, it responds to the request from the client socket and creates a new thread, send the description of the server socket to the client. Once the client confirms the description, both parties establish a connection. The server socket continues to be in the listening status, and continues to receive connection requests from other client sockets.

 

HTTP link features

The HTTP Protocol, Hypertext Transfer Protocol, is the foundation of Web networking and one of the commonly used protocols for mobile phone networking. HTTP is an application built on the TCP Protocol.

The most notable characteristic of HTTP connection is that each request sent by the client requires a server to send a response. After the request ends, the connection is released. The process from establishing a connection to closing a connection is called "One connection ".

 

 

The Internet Protocol (Protocol)

An IP address is a 32-bit unsigned integer. IP addresses are mapped to Domain names through the DNS (Domain Name System) Database

#!/usr/bin/- Chapter  -= =, hostname, , addr
# The address of google.com is 173.194.72.113

 

 

Python network programming:
Python provides all the methods to access the Socket interface of the underlying operating system, as well as a set of encryption and authentication communication services, SSL/TLS.

Sockets is actually a file descriptor. Unlike local files, Sockets connects to a file on the network.

1. Create a UDP local connection:

== 65535= 1060 sys.argv[1:] == [ = , address, , repr(data)              
     s.sendto( % sys.argv[1:] == [ , ( = s.recvfrom(MAX) , address, >>sys.stderr,

Run this Code:

, 1060, 62892, 1060) says , 62892) says 

2. Create a remote connection and verify the received information:

== 65535= 1060 2 <= len(sys.argv) <= 3  sys.argv[1] == = sys.argv[2]  len(sys.argv) > 2   = random.randint(0, 1 , address,  %  len(sys.argv) == 3  sys.argv[1] == = sys.argv[2 = 0.1     , delay, =*= 2               delay > 2.0 RuntimeError(                     >>sys.stderr,      >>sys.stderr, 2)

S. connect (hostname, PORT) method, so that we do not need to call s every time. sendto ('This is my message', ('2017. 0.0.1 ', PORT )). Direct call

S. send ('this is another message ').

 

 

Related Article

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.