Python 网络编程简单实现
TCP 入门
服务端:
import socket
"""
TCP 服务器端
"""
# 参数一:socket.AF_INET 默认配置,代表ipv4,AF_INET6,代表ipv6
# 参数二:socket.SOCK_STREAM 默认配置,代表TCP连接,socket.SOCK_DGRAM 代表UDP
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
port = 58124
s.bind((host, port)) # 绑定本地 ip 和 端口
s.listen(128) # 设置最大连接数
# 返回一个包含了 socket 和 客户端地址 的元组
resp_data = s.accept() # 简单写法: c, addr = s.accept()
print(f'resp_data type is {type(resp_data)} , resp_data is {resp_data}')
resp_socket = resp_data[0]
# recv(): 设置缓冲大小
print(resp_socket.recv(1024).decode('utf-8'))
resp_socket.send('谢谢, 我很好'.encode('utf-8'))
resp_socket.close()
# out:
"""
resp_data type is <class 'tuple'> , resp_data is (<socket.socket fd=132, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('192.168.29.21', 58124), raddr=('192.168.29.21', 63230)>, ('192.168.29.21', 63230))
你好啊
"""
客户端:
import socket
"""
TCP客户端
"""
s = socket.socket()
host = socket.gethostname()
port = 58124
s.connect((host, port))
s.send('你好啊'.encode('utf-8'))
print(s.recv(1024).decode('utf-8'))
# out:谢谢, 我很好
UDP 入门
服务方:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
host = socket.gethostname()
port = 12345
s.bind((host, port))
# 接收数据,resp_data 是发送的二进制数据内容
resp_data, caddr = s.recvfrom(1024) # 会阻塞
print(f'resp_data type is {type(resp_data)}, content is {resp_data.decode("utf-8")} \n '
f'caddr type is {type(caddr)}, content is {caddr}')
# 关闭套接字
s.close()
"""
resp_data type is <class 'bytes'>, content is 你好啊
caddr type is <class 'tuple'>, content is ('192.168.29.21', 61209)
"""
客户端:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
host = socket.gethostname()
# s.bind((host, 11111)) 客户端可以进行端口绑定,否则客户端端口每次请求会变化
# 指定要发送到哪里,
s.sendto('你好啊'.encode('utf-8'), (host, 12345))
s.close()
UDP 广播
发送端:
# 导包
import socket
"""UDP 发送端"""
# 建立套接字
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# 广播地址默认不允许访问,需要开通权限
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, True)
# 发送数据,广播地址还可以写为<broadcast>,255.255.255.255
# 当发送地址写为<broadcast>, 255.255.255.255 时, 接收端显示的发送端的ip是广域网的ip
# 当发送地址写为192.168.29.255 时, 接收端显示的发送端的ip是局域网的ip
s.sendto('你好啊'.encode('utf-8'), ('192.168.29.255', 12345))
s.sendto('你好啊啊'.encode('utf-8'), ('<broadcast>', 12345))
s.sendto('你好啊啊啊'.encode('utf-8'), ('255.255.255.255', 12345))
# 关闭连接
s.close()
接收端:
import socket
"""UDP 接收端"""
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
port = 12345
s.bind(('0.0.0.0', port))
# 接收数据,resp_data 是发送的二进制数据内容
resp_data, caddr = s.recvfrom(1024)
print(f'resp_data type is {type(resp_data)}, content is {resp_data.decode("utf-8")} \n '
f'caddr type is {type(caddr)}, content is {caddr}')
resp_data1, caddr1 = s.recvfrom(1024)
print(f'resp_data type is {type(resp_data1)}, content is {resp_data1.decode("utf-8")} \n '
f'caddr type is {type(caddr1)}, content is {caddr1}')
resp_data2, caddr2 = s.recvfrom(1024)
print(f'resp_data type is {type(resp_data2)}, content is {resp_data2.decode("utf-8")} \n '
f'caddr type is {type(caddr2)}, content is {caddr2}')
# 关闭套接字
s.close()
# out:
"""
resp_data type is <class 'bytes'>, content is 你好啊
caddr type is <class 'tuple'>, content is ('192.168.29.21', 58774)
resp_data type is <class 'bytes'>, content is 你好啊啊
caddr type is <class 'tuple'>, content is ('10.254.3.12', 58774)
resp_data type is <class 'bytes'>, content is 你好啊啊啊
caddr type is <class 'tuple'>, content is ('10.254.3.12', 58774)
"""
总结:
网络接口:
IP地址:
参考资料:
- 0.0.0.0 和 255.255.255.255 区别:
https://www.zhihu.com/question/267097519/answer/318401587?utm_medium=social
- PPP 适配器和以太网适配器区别:
本文来自博客园,作者:chuangzhou,转载请注明原文链接:https://www.cnblogs.com/czzz/p/15821432.html