The 23rd day arp protocol principle based on tcp protocol socket communication join the communication cycle and link cycle to write a program for remote execution of commands

How the arp protocol works

When the sender and receiver are in a local area network, the
arp protocol will calculate the ip address and subnet mask of the sender and the sender in a bitwise manner to get a subnet address. Comparing the subnet addresses can confirm whether the two
computers are in one. In the subnet, if two computers are in the same subnet, the arp protocol will pack the sender's mac address and pack the
target mac into FF:FF:FF:FF:FF:FF (refers to It needs the mac address, which is a signal). After the Ethernet header is packaged
, the ip header will be packaged again. The ip header is the ip header of the sender and the sender. Then the data packet (the content of the data packet is some
requests to obtain the mac address of the other party) will be sent to all computers in a broadcast manner . After all the computers in the intranet receive the packaged data packet,
compare the ip header, if the computer finds that the ip address sent by the sender is different from its own ip address, it directly deletes the data packet.
If it is found that the ip sent by the sender is the same as its own ip address, it will respond with its own mac address of the arp protocol. After both computers
know each other's mac address, they will interact with the data that needs to be sent through the mac address in the internal network.

When the sender and receiver are not in the same local area network, if the
arp protocol finds that the subnet address is different through bitwise operations, it will pack the sender's mac address and pack the target mac into FF:FF:FF:FF:FF:FF sent to the gateway,
after the Ethernet header packed, and then will head packing ip, ip ip header is the sender of the head and the gateway internal ip header, after receipt of the packet gateway, the gateway will own mac address sent
to arp protocol. After request a mac address of the gateway, the computer sends out the packet will need to throw the gateway, the gateway receives data, opened, will be the sender's
address into the LAN address of the public network ip external network, The gateway sends the routing protocol to the outside through the other party's public network ip address.

IP addresses are divided into public network ip and private network ip. Public network ip is used on the Internet. You can only connect to the Internet anywhere and you can access public network ip, while private ip
is used by local area network and cannot be accessed through the Internet. Access to private ip.
dns will resolve the domain name to the corresponding ip address,
dhcp will automatically obtain the dynamic ip address, the
network card is the mac address
, and the default server port number of the browser is 80

Write program client based on tcp protocol socket

import socket

# 1.买手机
phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # 流式协议,绑定ip和端口
# print(sever)

# 2.打电话
phone.connect(("127.0.0.1", 8080))  # 连接服务端

# 3.发收数据
phone.send("hello".encode("utf-8"))  # 网络发过来的数据都是bytes类型的数据
data = phone.recv(1024)
print(data.decode("utf-8"))

# 4.关闭
phone.close()

Server

import socket

# 1.买手机
phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # tcp协议的别称流式协议
# print(sever)

# 2.绑定电话卡
phone.bind(("127.0.0.1", 8080))  # 本地回环地址:127.0.0.1   绑定ip和端口

# 3.开机
phone.listen(5)  # 监听客户端发送的链接请求,半连接池最大为5

# 4.接收链接请求
conn, client_addr = phone.accept()   # conn封装了三次握手的成果,也就是双向的通路。client_addr是客户端的ip跟端口
# print(client_addr)

# 5.收发消息
data = conn.recv(1024)  # 最大接收的字节数,网络发过来的数据都是bytes类型的数据
# print(data)
conn.send(data.upper())

# 6.挂电话
conn.close()

# 7.关机
phone.close()

Add link cycle and communication cycle to write programs

Client

import socket

phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

phone.connect(("127.0.0.1", 8080))

while True:
    msg = input("输入:").strip()
    if len(msg) == 0:
        continue
    phone.send(msg.encode("utf-8"))
    data = phone.recv(1024)
    print(data.decode("utf-8"))

phone.close()

Server

import socket

phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
phone.bind(("127.0.0.1", 8080))
phone.listen(5)

while True:  # 连接循环
    conn, client_addr = phone.accept()
    print(client_addr)
    while True:  # 通信循环
        try:
            data = conn.recv(1024)
            if len(data) == 0:  # 在linux系统中,如果客户端暴力关闭接口,conn.recv会一直会接收None,进入一个死循环
                break
            print(data)
            conn.send(data.upper())
        except Exception:
            break
    conn.close()

phone.close()

Write a program that executes commands remotely

Client

import socket

phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

phone.connect(("127.0.0.1", 8080))

while True:
    msg = input("输入:").strip()
    if len(msg) == 0:
        continue
    phone.send(msg.encode("utf-8"))
    data = phone.recv(1024)
    print(data.decode("gbk"))

phone.close()

Server

import socket
import subprocess

phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
phone.bind(("127.0.0.1", 8080))
phone.listen(5)

while True:
    conn, client_addr = phone.accept()
    print(client_addr)
    while True:
        try:
            data = conn.recv(1024)
            if len(data) == 0:
                break
            cmd = data.decode("utf-8")
            obj = subprocess.Popen(cmd, shell=True,
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE
                                   )
            res1 = obj.stdout.read()
            res2 = obj.stderr.read()

            conn.send(res1 + res2)
        except Exception:
            break
    conn.close()

Guess you like

Origin blog.csdn.net/Yosigo_/article/details/112758309