Socket网路编程

1.简单实例

import socket

client = socket.socket()  #声明Socket类型,同时生成socket连接对象
client.connect(('localhost',6969))
client.send(b'hello,world!')
data = client.recv(1024)
print("recv:",data)
client.close()
client
import socket

server = socket.socket()
server.bind(('localhost',6969)) # 绑定
server.listen() # 监听
print("等待电话")
#conn就是客户端连过来而在服务器端为其生成一个连接实例
conn,addr = server.accept()  #等待
print(conn,addr)
print("电话来了")
data = conn.recv(1024)
print("recv:",data)
conn.send(data.upper())
server.close()
View Code

 2.通过socket 实现简单的ssh  。客户端连接上服务器后,服务器执行命令,并返回结果给客户端

 1 import socket
 2 import os
 3 
 4 server = socket.socket()
 5 server.bind(('localhost',9969))
 6 server.listen()
 7 
 8 while True:
 9     conn,addr = server.accept()
10     print("new conn:",addr)
11     while True:
12         data = conn.recv(1024)
13         if not data:
14             print("客户端已经断开")
15             break
16         print("执行指令")
17         cmd_res = os.popen(data.decode("utf-8")).read()
18         if len(cmd_res) == 0:
19             cmd_res = "cmd has no output!"
20         #conn.send(cmd_res.encode("utf-8"))  # send这里发送数据,客户端接受的数据buf不够,把剩下的放在缓冲区,下一次调用send,假设这里的只有900多数据,但是这里只会先把缓冲区的发出去,
21                                             # 剩下的放在缓冲区,等待下次发送。所以会出现返回结果混乱的问题。缓存区可提高 io 效率
22         
23 server.close()
socket_server_ssh
 1 import socket
 2 
 3 client = socket.socket()
 4 client.connect(('localhost',9969))
 5 
 6 while True:
 7     cmd = input(">>:").strip()
 8     if len(cmd) == 0:continue
 9     client.send(cmd.encode('utf-8'))
10     cmd_res = client.recv(1024)
11     print(cmd_res.decode())
12 
13 client.close()
socket_client_ssh

 3.完整版简单ssh,解决数据混乱(即缓冲区可能一次不能发完的事件)

import socket
client = socket.socket()
client.connect(('localhost',6969))
while True:
    cmd = input(">>:").strip()
    if len(cmd) == 0:continue
    client.send(cmd.encode("utf-8"))
    cmd_res_size =client.recv(2048)
    print("接受命令执行结果的长度,",cmd_res_size)
    received_size = 0
    received_data = b''
    while received_size < int(cmd_res_size):
        data = client.recv(2048)
        received_size += len(data)
        print(received_size)
        received_data += data
    else:
        print("cmd res receive done....",received_size)
        print(received_data.decode())

client.close()

# 接受命令执行结果的长度, b'1141'
# 1141
# cmd res receive done.... 1141
# 
# Windows IP 配置
client_ssh
import socket
import os
server = socket.socket()
server.bind(('localhost',6969))
server.listen()

while True:
    conn,addr = server.accept()
    print("等待conn:",addr)
    while True:
        data = conn.recv(2048)
        print("收到")
        if not data:
            print("客户端已经断开")
            break
        print("执行指令",data.decode())
        cmd_res = os.popen(data.decode()).read()
        if len(cmd_res) == 0:
            cmd_res = "cmd has no out put"
        conn.send(str(len(cmd_res.encode("utf-8"))).encode("utf-8"))   #可能存在中文,因为中文占三个字节,所以这里先把结果encode,在len,str,encode
        conn.send(cmd_res.encode("utf-8"))

server.close()
server_ssh

 

posted @ 2018-10-29 20:56  杨fj  阅读(107)  评论(0编辑  收藏  举报