main content:
1. Software infrastructure
2. Network Basics
3. Socket (socket) first acquaintance
A software development framework
The applications we know that involve communication between two programs can be roughly divided into two categories:
The first is the application category: QQ, WeChat, Netdisk, Youku are desktop applications that need to be installed
The second is the web class: applications such as Baidu, Zhihu, Blog Park, etc. that can be used directly by using a browser to access
The essence of these applications is actually the communication between two programs. And these two categories correspond to two software development architectures~
1. C/S Architecture
C/S is: Client and Server, Chinese meaning: client and server architecture, this architecture is also divided from the user level (or the physical level).
The client here generally refers to the client application EXE. The program needs to be installed before it can run on the user's computer, which is highly dependent on the user's computer operating system environment.
2.B/S architecture
B/S is: Browser and Server, Chinese meaning: browser-side and server-side architecture, this architecture is divided from the user level.
Browser browser, in fact, is also a client client, but this client does not require you to install any application, just request server-side related resources (web page resources) through HTTP on the browser, and the client Browser browser will Able to add, delete, modify and check.
Second, the network foundation
1. How does one program find another program on the web?
First of all, the program must be started, and secondly, the address of this machine must be available. We all know that the address of our people is probably the word country\province\city\district\street\lou\house number. Then every networked machine also has its own address on the network. How is its address represented?
It is represented by a string of numbers, for example: 100.4.5.6
IP address refers to the Internet Protocol Address (English: Internet Protocol Address, also translated as Internet Protocol Address), which is the abbreviation of IP Address. The IP address is a unified address format provided by the IP protocol. It assigns a logical address to each network and each host on the Internet, thereby shielding the difference in physical addresses.
An IP address is a 32-bit binary number, usually divided into 4 "8-bit binary numbers" (that is, 4 bytes). IP addresses are usually expressed in the form of "dotted decimal" (abcd), where a, b, c, and d are all decimal integers between 0 and 255. Example: The dotted decimal IP address (100.4.5.6) is actually a 32-bit binary number (01100100.00000100.00000101.00000110).
" Port " is the free translation of English port, which can be considered as the export of communication between equipment and the outside world.
Therefore, the ip address is accurate to a specific computer, and the port is accurate to a specific program.
2.OSi seven-layer model
Introduction
It should be noted that a complete computer system is composed of hardware, operating system, and application software. With these three conditions, a computer system can play with itself (play a stand-alone game, play minesweeper or something)
If you want to play with other people, then you need to be on the Internet, what is the Internet?
The core of the Internet is composed of a bunch of protocols, and the protocol is the standard. For example, the standard of communication for people all over the world is English. If you compare computers to people, the Internet protocol is the English of the computer world. All computers have learned the Internet Protocol, so all computers can send and receive information according to a unified standard to complete communication.
osi seven-layer model
People logically divide Internet protocols into layers according to the division of labor:
3.socket concept
socket layer
understand socket
Socket is a middleware abstraction layer that communicates between the application layer and the TCP/IP protocol suite. It is a set of interfaces. In the design mode, Socket is actually a facade mode, which hides the complex TCP/IP protocol family behind the Socket interface. For users, a set of simple interfaces is all, and let the Socket organize the data to meet the specified requirements. protocol
In fact, from your point of view, a socket is a module. We establish the connection and communication between the two processes by calling the methods already implemented in the module. Some people also say socket as ip + port, because ip is used to identify the location of a host on the Internet, and port is used to identify an application on this machine. So we can find an application as long as we establish the ip and port, and use the socket module to communicate with it.
3. The development history of sockets
Sockets originated in the 1970s version of Unix at the University of California, Berkeley, known as BSD Unix. Therefore, sockets are sometimes referred to as "Berkeley sockets" or "BSD sockets". Originally, sockets were designed for communication between multiple applications on the same host. This is also called inter-process communication, or IPC. There are two types of sockets (or two races), file-based and network-based.
File type based socket family
Socket family name: AF_UNIX
Everything in unix is a file. The file-based socket calls the underlying file system to fetch data. Two socket processes run on the same machine and can communicate indirectly by accessing the same file system.
Socket family based on network type
Socket family name: AF_INET
(Also AF_INET6 is used for ipv6, and there are a few other address families, however, they are either platform-only, deprecated, rarely used, or not implemented at all, all addresses Among the families, AF_INET is the most widely used one. Python supports many address families, but since we only care about network programming, we only use AF_INET most of the time)
4.tcp protocol and udp protocol
TCP (Transmission Control Protocol) reliable, connection-oriented protocol (eg: making phone calls), low transmission efficiency, full-duplex communication (send buffer & receive buffer), byte stream-oriented. Applications that use TCP: Web browsers; email, file transfer programs.
UDP (User Datagram Protocol) unreliable, connectionless service, high transmission efficiency (small delay before sending), one-to-one, one-to-many, many-to-one, many-to-many, message-oriented, best effort service , without congestion control. Applications using UDP: Domain Name System (DNS); Video Streaming; Voice over IP (VoIP).
I know that you don't understand this, so just upload the picture.
4. Initial use of sockets
socket based on TCP protocol
tcp is based on the link, you must start the server first, and then start the client to link the server
server side
import socket sk = socket.socket() sk.bind(( ' 127.0.0.1 ' ,8898)) #Bind the address to the socket sk.listen() #Listen to the connection conn,addr = sk.accept() #Accept the client connection ret = conn.recv (1024) #Receive client information print (ret) #Print client information conn.send(b ' hi ' ) #Send information to the client conn.close() #Close the client socket sk.close( ) # close server socket (optional)
client side
import socket sk = socket.socket() #Create a client socket sk.connect(( ' 127.0.0.1 ' ,8898)) #try to connect to the server sk.send(b'hello ! ' ) ret = sk.recv(1024) #Dialog ( send /receive) print (ret) sk.close() #Close the client socket
Question: Some students may encounter problems when restarting the server
#Add a socket configuration, reuse ip and port import socket from socket import SOL_SOCKET, SO_REUSEADDR sk = socket.socket() sk.setsockopt(SOL_SOCKET,SO_REUSEADDR, 1) # That's it, add sk.bind(( ' 127.0.0.1 ' ,8898)) #Bind the address to the socket sk.listen() #Listen to the link conn ,addr = sk.accept() #Accept client link ret = conn.recv(1024) #Receive client information print (ret) #Print client information conn.send(b ' hi ' ) # Send information to client conn.close() #Close the client socket sk.close() #Close the server socket (optional)
Socket based on UDP protocol
udp is unconnected. After starting the service, you can directly receive messages without establishing a link in advance.
Simple to use
server side
import socket udp_sk = socket.socket(type=socket.SOCK_DGRAM) #Create a server socket udp_sk.bind(('127.0.0.1',9000)) #Bind server socket msg,addr = udp_sk.recvfrom(1024) print(msg) udp_sk.sendto(b'hi',addr) # Conversation (receive and send) udp_sk.close() # close the server socket
client side
import socket ip_port=('127.0.0.1',9000) udp_sk=socket.socket(type=socket.SOCK_DGRAM) udp_sk.sendto(b'hello',ip_port) back_msg,addr=udp_sk.recvfrom(1024) print(back_msg.decode('utf-8'),addr)
qq chat
#_*_coding:utf-8_*_ import socket ip_port=('127.0.0.1',8081) udp_server_sock = socket.socket (socket.AF_INET, socket.SOCK_DGRAM) udp_server_sock.bind(ip_port) while True: qq_msg,addr=udp_server_sock.recvfrom(1024) print('A message from [%s:%s]:\033[1;44m%s\033[0m' %(addr[0],addr[1],qq_msg.decode('utf-8') )) back_msg=input('Reply message: ').strip() udp_server_sock.sendto(back_msg.encode('utf-8'),addr)
#_*_coding:utf-8_*_ import socket BUFSIZE=1024 udp_client_socket=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) qq_name_dic={ 'Boss Jin':('127.0.0.1',8081), 'Nezha ':('127.0.0.1',8081), 'egg':('127.0.0.1',8081), 'yuan':('127.0.0.1',8081), } while True: qq_name=input('Please Select the chat object: ').strip() while True: msg=input('Please enter the message, press Enter to send, enter q to end the chat with him: ').strip() if msg == 'q':break if not msg or not qq_name or qq_name not in qq_name_dic:continue udp_client_socket.sendto(msg.encode('utf-8'),qq_name_dic[qq_name]) back_msg,addr=udp_client_socket.recvfrom(BUFSIZE) print('From [%s: A message for %s]:\033[1;44m%s\033[0m' %(addr[0],addr[1],back_msg.decode('utf-8'))) udp_client_socket.close()
time server
# _*_coding:utf-8_*_
from socket import *
from time import strftime
ip_port = ('127.0.0.1', 9000)
bufsize = 1024
tcp_server = socket(AF_INET, SOCK_DGRAM)
tcp_server.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)
tcp_server.bind(ip_port)
while True:
msg, addr = tcp_server.recvfrom(bufsize)
print('===>', msg)
if not msg:
time_fmt = '%Y-%m-%d %X'
else:
time_fmt = msg.decode('utf-8')
back_msg = strftime(time_fmt)
tcp_server.sendto(back_msg.encode('utf-8'), addr)
tcp_server.close()
server
#_*_coding:utf-8_*_
from socket import *
ip_port=('127.0.0.1',9000)
bufsize=1024
tcp_client = socket (AF_INET, SOCK_DGRAM)
while True:
msg=input('Please input the time format (eg %Y %m %d)>>: ').strip()
tcp_client.sendto(msg.encode('utf-8'),ip_port)
data=tcp_client.recv(bufsize)
client
Detailed explanation of socket parameters
socket.socket(family=AF_INET,type=SOCK_STREAM,proto=0,fileno=None)
创建socket对象的参数说明:
family | The address family should be AF_INET (default), AF_INET6, AF_UNIX, AF_CAN or AF_RDS. (AF_UNIX domains actually use local socket files to communicate) |
type | The socket type should be SOCK_STREAM (default), SOCK_DGRAM, SOCK_RAW or one of the other SOCK_ constants. SOCK_STREAM is a connection-oriented SOCKET based on TCP, which is guaranteed (that is, it can ensure that the data is correctly transmitted to the other party), and is mostly used for data transmission. SOCK_DGRAM is a UDP-based, non-guaranteed message-oriented socket, which is mostly used to broadcast information on the network. |
proto | The protocol number is usually zero and can be omitted, or in case the address family is AF_CAN, the protocol should be one of CAN_RAW or CAN_BCM. |
fileno | If fileno is specified, other arguments are ignored, causing a socket with the specified file descriptor to return. Unlike socket.fromfd(), fileno will return the same socket, not a duplicate. This may help to close a standalone socket using socket.close(). |