The network is divided into physical layer, data link layer, network layer, transport layer, session layer, presentation layer and application layer from bottom to top.
HTTP is a high-level protocol, while TCP/IP is a protocol set that includes many sub-protocols. Including: FTP, UDP, TCP protocols at the transport layer, ip protocol at the network layer, etc., high-level protocols such as HTTP, telnet protocol, etc. HTTP is a sub-protocol of TCP/IP.
Socket is the encapsulation and application of the TCP/IP protocol (at the programmer level). It can also be said that TPC/IP protocol is a transport layer protocol, which mainly solves how data is transmitted in the network, while HTTP is an application layer protocol, which mainly solves how to package data.
When we transmit data, we can only use the (transport layer) TCP/IP protocol, but in that case, without the application layer, the data content cannot be identified. If you want to make the transmitted data meaningful, you must use the application There are many application layer protocols, such as HTTP, FTP, TELNET, etc. You can also define your own application layer protocols. WEB uses HTTP protocol 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 socket we usually talk about most? In fact, socket is an encapsulation of the TCP/IP protocol. Socket itself is not a protocol, but a calling interface (API). Through Socket, we can use TCP/IP protocol. In fact, Socket is not necessarily related to the TCP/IP protocol. When the Socket programming interface was designed, it was hoped that it could also adapt to other network protocols. Therefore, the emergence of Socket only makes it easier for programmers to use the TCP/IP protocol stack. It is an abstraction of the TCP/IP protocol, thus forming some of the most basic function interfaces we know, such as create, listen, connect, accept, send, read and write etc.
TCP/IP is just a protocol stack, just like the operating mechanism of the operating system, it must be implemented concretely, and it must also provide an external operation interface. Just like the operating system provides a standard programming interface, such as the win32 programming interface, TCP/IP also provides an interface that programmers can use for network development. This is the Socket programming interface.
There is a more vivid description: HTTP is a car, which provides a specific form of encapsulating or displaying data; Socket is an engine, which provides the capability of network communication.
In fact, the TCP of the transport layer is based on the IP protocol of the network layer, and the HTTP protocol of the application layer is based on the TCP protocol of the transport layer. Socket itself is not a protocol. As mentioned above, it only provides An interface for TCP or UDP programming.
Steps to establish a network connection using Socket:
Establishing a Socket connection requires at least a pair of sockets, one of which runs on the client, called ClientSocket, and the other runs on the server, called ServerSocket.
The connection process between sockets is divided into three steps: server monitoring, client request, and connection confirmation.
1. Server monitoring: The server-side socket does not locate the specific client socket, but is in a state of waiting for connection, monitoring the network status in real time, and waiting for the client's connection request.
2. Client request: refers to the client's socket making a connection request, and the target to be connected is the server's socket. To do this, the client's socket must first describe the server's socket to which it wants to connect, indicate the address and port number of the server-side socket, and then make a connection request to the server-side socket.
3. Connection confirmation: When the server-side socket listens or receives a connection request from the client socket, it responds to the client socket's request, establishes a new thread, and sends the description of the server-side socket. To the client, once the client confirms this description, the connection is officially established between the two parties. The server-side socket continues to be in the listening state and continues to receive connection requests from other client sockets.
Characteristics of HTTP links
HTTP protocol is the Hypertext Transfer Protocol, which is the basis of Web networking and one of the commonly used protocols for mobile phone networking. The HTTP protocol is an application built on the TCP protocol.
The most notable feature of HTTP connections is that each request sent by the client requires the server to send back a response. After the request is completed, the connection will be actively released. The process from establishing a connection to closing the connection is called "a connection".
The Internet Protocol
IP is a 32-bit unsigned integer. The IP address is mapped to the domain name through the DNS (Domain Name System) database
#!/usr/bin/env python # Foundations of Python Network Programming - Chapter 1 - getname.py import socket hostname = 'google.com' addr = socket.gethostbyname(hostname) print 'The address of', hostname, 'is', addr # The address of google.com is 173.194.72.113
Python network programming:
Python provides all methods to access the Socket interface of the underlying operating system, and also provides a set of encrypted and authenticated communication services, SSL/TLS .
Sockets is actually a file descriptor. Different from local files, it connects to a file on the network.
1. Create a UDP local connection:
#!/usr/bin/env python import socket, sys s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) MAX = 65535 PORT = 1060 if sys.argv[1:] == ['server']: s.bind(('127.0.0.1', PORT)) print 'Listening at', s.getsockname() while True: data, address = s.recvfrom(MAX) print 'The client at', address, 'says', repr(data) s.sendto('Your data was %d bytes' % len(data), address) elif sys.argv[1:] == ['client']: print 'Address before sending:', s.getsockname() s.sendto('This is my message', ('127.0.0.1', PORT)) print 'Address after sending', s.getsockname() data, address = s.recvfrom(MAX) # overly promiscuous - see text! print 'The server', address, 'says', repr(data) else: print >>sys.stderr, 'usage: udp_local.py server|client'
Run this code:
python filename.py server #Listening at ('127.0.0.1', 1060) #Address before sending: ('0.0.0.0', 0) #Address after sending ('0.0.0.0', 62892) #The server ('127.0.0.1', 1060) says 'Your data was 18 bytes' python filename.py client #The client at ('127.0.0.1', 62892) says 'This is my message'
2. Create a remote connection and verify the received information:
import random, socket, sys s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) MAX = 65535 PORT = 1060 if 2 <= len(sys.argv) <= 3 and sys.argv[1] == 'server': interface = sys.argv[2] if len(sys.argv) > 2 else '' s.bind((interface, PORT)) print 'Listening at', s.getsockname() while True: data, address = s.recvfrom(MAX) if random.randint(0, 1): print 'The client at', address, 'says:', repr(data) s.sendto('Your data was %d bytes' % len(data), address) else: print 'Pretending to drop packet from', address elif len(sys.argv) == 3 and sys.argv[1] == 'client': hostname = sys.argv[2] s.connect((hostname, PORT)) print 'Client socket name is', s.getsockname() delay = 0.1 while True: s.send('This is another message') print 'Waiting up to', delay, 'seconds for a reply' s.settimeout(delay) try: data = s.recv(MAX) except socket.timeout: delay *= 2 # wait even longer for the next request if delay > 2.0: raise RuntimeError('I think the server is down') else: break # we are done, and can stop looping print 'The server says', repr(data) else: print >>sys.stderr, 'usage: udp_remote.py server [ <interface> ]' print >>sys.stderr, ' or: udp_remote.py client <host>' sys.exit(2)
这里的s.connect((hostname, PORT))方法,可以让我们不用每次都调用s.sendto('This is my message', ('127.0.0.1', PORT))。直接调用
s.send('This is another message')。

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.

Python's real-world applications include data analytics, web development, artificial intelligence and automation. 1) In data analysis, Python uses Pandas and Matplotlib to process and visualize data. 2) In web development, Django and Flask frameworks simplify the creation of web applications. 3) In the field of artificial intelligence, TensorFlow and PyTorch are used to build and train models. 4) In terms of automation, Python scripts can be used for tasks such as copying files.

Python is widely used in data science, web development and automation scripting fields. 1) In data science, Python simplifies data processing and analysis through libraries such as NumPy and Pandas. 2) In web development, the Django and Flask frameworks enable developers to quickly build applications. 3) In automated scripts, Python's simplicity and standard library make it ideal.

Python's flexibility is reflected in multi-paradigm support and dynamic type systems, while ease of use comes from a simple syntax and rich standard library. 1. Flexibility: Supports object-oriented, functional and procedural programming, and dynamic type systems improve development efficiency. 2. Ease of use: The grammar is close to natural language, the standard library covers a wide range of functions, and simplifies the development process.

Python is highly favored for its simplicity and power, suitable for all needs from beginners to advanced developers. Its versatility is reflected in: 1) Easy to learn and use, simple syntax; 2) Rich libraries and frameworks, such as NumPy, Pandas, etc.; 3) Cross-platform support, which can be run on a variety of operating systems; 4) Suitable for scripting and automation tasks to improve work efficiency.

Yes, learn Python in two hours a day. 1. Develop a reasonable study plan, 2. Select the right learning resources, 3. Consolidate the knowledge learned through practice. These steps can help you master Python in a short time.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool