服务器端:

流程:

  1.创建servert实例

  2.绑定地址和端口

  3.开始监听

  4.创建客户端连接实例

  5.等待客户端的消息

  6..........

 # The_author = 'liu66'
# By python3.x
# -*- coding = utf-8 -*- import socket,os server=socket.socket()
server.bind(("localhost",66))#绑定要监听的端口
server.listen(5)#开始监听
# 等电话打进来 while True:
print("我要开始等电话了")
conn,addr=server.accept()#conn为连接的实例
print("实例的连接和地址分别为:",conn,addr)
while True:
print("等待数据接收...")
data=conn.recv(1024)
if not data:
print("client has closed...")
break
# print(data)
# print(data.decode())
# conn.send(data.upper())
# 执行cmd命令
try:
res = os.popen(data.decode()).read()
except TypeError:
print("请输入正确的CMD")
else:
print("------------------")
print(res)#打印返回的值
print("------------------")
'''一定要判断返回的长度'''
print(len(res))
received_size=''
if len(res) == 0:
res = "cmd is not correct..."
print(res)
conn.send(str(len(res.encode("utf-8"))).encode()) sleep_recv = conn.recv(1024)
print("缓冲...", sleep_recv.decode()) # 两条send命令可能会出现粘包的情况 conn.send(res.encode("utf-8")) print("server is closed...")
server.close()

ssh_server

客户端:

  1.创建client实例

  2.连接服务器,地址端口

  3.向服务器发送命令

  4............

 # The_author = 'liu66'
# By python3.x
# -*- coding = utf-8 -*- import socket client=socket.socket()
client.connect(("localhost",66)) while True:
send_data=input(">>:").strip()
if len(send_data) == 0: continue
client.send(send_data.encode("utf-8"))
# data=client.recv(1024)
# print("server接收到的数据:",data.decode()) ############ 数据初始化 ####################
data_size=0#要接受的数据大小
data_res=b''#要接受的数据的内容大小
received_size=0
received_data=b''#要接受的数据的内容
############################################
'''先接收要接受的数据大小'''
data_size=client.recv(1024)
print("要接受的数据大小:", data_size) '''发送缓冲数据'''
client.send("...".encode()) while received_size != int(data_size):
data_res=client.recv(1024)
received_size+=len(data_res)
received_data+=data_res
else:
print("已接收数据大小:",received_size)
print(received_data.decode("utf-8","ignore"))
'''decode()默认的参数就是strict,代表遇到非法字符时抛出异常;
如果设置为ignore,则会忽略非法字符;
如果设置为replace,则会用?号取代非法字符;
如果设置为xmlcharrefreplace,则使用XML的字符引用。''' # print(data_res.decode("utf-8","replace")) print("client is closed...")
client.close()

ssh_client

问题总结:

  1. 绑定地址和端口时,需要合并为一个参数
client.connect(("localhost",66))

  2.  发送cmd时,判断发送的字符串不能为空

send_data=input(">>:").strip()
if len(send_data) == 0: continue

  3.  发送数据需要进行编码encode

client.send(send_data.encode("utf-8"))

  4.  先发送需要的字符长度,再接收一个缓冲消息,最后再发送内容,防止两次连续发送的粘包

 conn.send(str(len(res.encode("utf-8"))).encode())
sleep_recv = conn.recv(1024)
print("缓冲...", sleep_recv.decode()) # 两条send命令可能会出现粘包的情况 conn.send(res.encode("utf-8"))

  5.  执行命令异常处理

        try:
res = os.popen(data.decode()).read()
except TypeError:
print("请输入正确的CMD")
else:
print("------------------")
print(res)#打印返回的值
print("------------------")

  6.  发送ls命令不报error,返回为空,判断返回是否为空

            print(len(res))
received_size=''
if len(res) == 0:
res = "cmd is not correct..."

  7.  对于数据超过1024时处理,判断每次接受的字节长度,累加,直到长度等于发送的总长度

    while received_size != int(data_size):
data_res=client.recv(1024)
received_size+=len(data_res)
received_data+=data_res

  8.  print(xx.decode())经常出现非法字符异常的解决办法

        print(received_data.decode("utf-8","ignore"))
'''decode()默认的参数就是strict,代表遇到非法字符时抛出异常;
如果设置为ignore,则会忽略非法字符;
如果设置为replace,则会用?号取代非法字符;
如果设置为xmlcharrefreplace,则使用XML的字符引用。'''

最新文章

  1. js小技巧
  2. gnuplot使用,操作,保存等教程
  3. MySQL MEM_ROOT详细讲解
  4. HTML5中canvas大小调整
  5. MyEclipse中如何安装插件(以Subclipse为例)[转]
  6. html的<!DOCTYPE>标签初窥
  7. SSM框架学习之高并发秒杀业务--笔记1-- 项目的创建和依赖
  8. SSM框架——以注解形式实现事务管理
  9. esriSRProjCS4Type Constants
  10. 【MVC】关于Action返回结果类型的事儿(上)
  11. 微软Azure云主机及blob存储的网络性能测试
  12. 某Python群的入群题目
  13. Assumptions
  14. js--闭包的理解
  15. 今天筹备了一件大事:重学JS
  16. ubuntu:基本操作;
  17. DAX/PowerBI系列 - 玩转阿里云 Alicloud Pricing
  18. 【转】对cocos2d 之autorelease\ratain\release的理解
  19. ASP.NET MVC 5搭建自己的视图基架 (CodeTemplate)
  20. android通过USB使用真机调试程序

热门文章

  1. 浏览器与CDN缓存行为
  2. Flume环境搭建_五种案例
  3. vim&vi在编辑的时候突然卡死,不接收输入问题的解决
  4. WdatePicker时间插件
  5. SQLite学习手册(实例代码<一>)
  6. lodash源码分析之缓存使用方式的进一步封装
  7. java—— finall 关键词
  8. junit测试延伸--私有方法测试
  9. 自己模拟的一个简单的tomcat
  10. MyEclipse中好用的快捷键汇总