- socket简单介绍
# Author : Xuefeng
# ipconfig /all 查看IP地址与Mac(物理)地址
import socket
# socket family(地址簇)
# socket.AF_UNIX # 本机两个进程之间进行通信
# socket.AF_INET # IPV4
# socket.AF_INET6 # IPV6
# socket types(协议类型)
# socket.SOCK_STREAM # TCP
# socket.SOCK_DGRAM # UDP
# socket.SOCK_RAW # 伪造IP
- 服务端
# Author : Xuefeng
# 服务器端
import socket
import os
server = socket.socket()
server.bind(("Localhost", 6969)) # 绑定要监听的端口
server.listen() # 监听
while True:
print("我要开始等电话了。。。")
conn, addr = server.accept() # 等电话打进来
# conn就是客户端连过来而在服务器端为其生成的一个连接实例
print(conn, addr)
print("电话来了。。。")
while True:
data = conn.recv(1024)
print("recv:", data)
# conn.send(data.upper())
# ssh
res = os.popen(data).read()
conn.send(res)
server.close()
- 客户端
# Author : Xuefeng
# 客户端
import socket
client = socket.socket() # 声明socket类型, 同时生成socket连接对象
client.connect(("Localhost", 6969))
while True:
msg = input(">>:").strip()
client.send(msg.encode(encoding = "utf-8"))
data = client.recv(1024)
print("recv:", data.decode(encoding="utf-8"))
client.close()