Implementation of Python Socket communication

Implementation of Python Socket communication

1. Introduction to Socket Communication

  1. Two programs on the network exchange data through a two-way communication connection, and one end of this connection is called a socket.

  2. Establishing a network communication connection requires at least a pair of port numbers (sockets). The essence of socket is a programming interface (API), which encapsulates TCP/IP. TCP/IP also provides an interface for programmers to do network development. This is the Socket programming interface; HTTP is a car, which provides a specific form of packaging or displaying data;

  3. The English original meaning of Socket is "hole" or "socket". As a communication mechanism, take the latter meaning. Usually also called "socket", it is used to describe IP addresses and ports, and is a handle of a communication chain, which can be used to realize communication between different virtual machines or different computers. Hosts on the Internet generally run multiple service software and provide several services at the same time. Each service opens a Socket and binds to a port, and different ports correspond to different services. Socket is just like its original meaning in English, like a porous socket. A mainframe is like a room full of various sockets, each socket has a number, some sockets provide 220 volts of alternating current, some provide 110 volts of alternating current, and some provide cable TV programs. The client software can get different services by inserting the plugs into sockets with different numbers.

  4. Socket is an intermediate software abstraction layer for communication between the application layer and the TCP/IP protocol family, and 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 Socket organizes data to conform to the specified protocol.

2. Code implementation

Server implementation

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 19-1-16 上午8:22
# @Author  : Fang
# @E-mail  : [email protected]
# @Site    : 
# @File    : html_socket.py
# @Software: PyCharm


import socket

# 1、创建服务端的socket对象
sk = socket.socket()

# 2、绑定一个ip和端口
sk.bind(("127.0.0.1",8888))

# 3、服务器端一直监听是否有客户端进行连接
sk.listen(5)

while 1:
    # 4、如果有客户端进行连接、则接受客户端的连接
    conn,addr = sk.accept() # 返回客户端socket通信对象和客户端的ip

    # 5、客户端与服务端进行通信
    rev_data = conn.recv(1024)
    print('服务端收到客户端发来的消息:%s' % (rev_data.decode('GB2312')))

    # 6、服务端给客户端回消息
    conn.send(b"HTTP/1.1 200 OK \r\n\r\n")  #http协议
    show_str = "<h1> 这短短的一生,我们最终都会失去,你不妨大胆一些。爱一个人,攀一座山,追一个梦,加油 !!!</h1>"
    conn.send(show_str.encode('GB2312'))

    # 7、关闭socket对象
    conn.close()

client implementation

We can write the client by ourselves, or directly use the browser, that is, the B/S architecture

Access http://127.0.0.1:8000 with a browser

insert image description here

Next, we write a client to send and receive data to each other.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 19-1-16 下午2:56
# @Author  : Fang
# @E-mail  : [email protected]
# @Site    : 
# @File    : scoket_client.py
# @Software: PyCharm

import socket

# 1、创建socket通信对象
clientSocket = socket.socket()

# 2、使用正确的ip和端口去链接服务器
clientSocket.connect(("127.0.0.1",8888))

# 3、客户端与服务器端进行通信
# 给socket服务器发送信息
send_data = "你拼命赚钱的样子虽然有些狼狈。但是自己靠自己的样子真的很美!加油"
clientSocket.send(send_data.encode('GB2312'))

# 接收服务器的响应(服务器回复的消息)
recvData = clientSocket.recv(1024).decode('GB2312')
print('客户端收到服务器回复的消息:%s' % (recvData))

# 4、关闭socket对象
clientSocket.close()

run it

run server
insert image description here

Run the client, the client receives the data sent by the server
insert image description here

Look at the server also received the data from the client
insert image description here

Summarize

The B/S architecture is very convenient, and it is a change or improved architecture to the C/S architecture. Compared with the C/S architecture, there is no need to install an additional client, and there is no client upgrade, which saves a lot of trouble

Of course, don’t be led astray. At the beginning, I just wanted to be lazy and not write the client, so I thought about using a browser instead of the client. Of course, this is not the correct usage of the B/S architecture and socket.

Guess you like

Origin blog.csdn.net/Running_free/article/details/86509305