发送端伪代码
import socket
socket.TCP/IP
connect(a.ip, a.port)
socket.send(hello)
socket.recv()
socket.close()
接收端伪代码
import socket
socket.TCP/IP
listen(0.0.0.0, 6969)
waiting()
recv()
send()
简单的socket接受、发送实例
服务器端:
# Author:dancheng
#服务器端
import socket
server = socket.socket()
server.bind(('localhost', 6969)) #绑定要坚听的端口
server.listen(5) #监听
while True:
#print('1')
#conn就是客户端连过来而在服务器端为其生成的一个连接实例
conn, addr = server.accept() #等待
# print(conn, addr)
# print('2')
while True:
data = conn.recv(1024)
print('recv:', data)
if not data:
break
conn.send(data.upper())
server.close()
客户端:
#声明协议类型,同时生成socket连接对象
client = socket.socket()
client.connect(('localhost', 6969))
while True:
msg = input('>>:').strip()
if len(msg) == 0:
continue
client.send(msg.encode('utf-8'))
data = client.recv(1024)
print('recv:', data.decode())
client.close()
实现简单的ssh软件操作
有时会出现粘包问题。
服务端代码:
# Author:dancheng
import socket, os
server = socket.socket()
server.bind(('localhost', 9999))
server.listen()
while True:
conn, addr = server.accept()
print("new conn:",addr)
while True:
data = conn.recv(1024)
if not data:
print("客户端以断开")
break
print("执行指令:",data)
cmd_res = os.popen(data.decode()).read() #接收字符串,执行结果也是字符串
if len(cmd_res) == 0:
cmd_res = 'cmd has no output...'
conn.send(str(len(cmd_res)).encode('utf-8')) #先给客户端发送文件大小
conn.send(cmd_res.encode('utf-8'))
server.close()
客户端代码:
# Author:dancheng
import socket
client = socket.socket()
client.connect(('localhost', 9999))
while True:
cmd = input(">>:").strip()
if len(cmd) == 0:continue
client.send(cmd.encode('utf-8'))
cmd_res_size = client.recv(1024) #接收命令结果的长度
print("命令长度:",cmd_res_size)
received_size = 0
received_data = b''
while received_size < int(cmd_res_size.decode()):
data = client.recv(1024)
received_size += len(data) #每次收到的有可能小于1024,所以必须用len判断
received_data += data
else:
print("cmd res receive done...", received_size)
print(received_data.decode())
client.close()
解决粘包后的程序:
服务端代码:
# Author:dancheng
import socket, os, time
server = socket.socket()
server.bind(('localhost', 9999))
server.listen()
while True:
conn, addr = server.accept()
print("new conn:",addr)
while True:
data = conn.recv(1024)
if not data:
print("客户端以断开")
break
print("执行指令:",data)
cmd_res = os.popen(data.decode()).read() #接收字符串,执行结果也是字符串
if len(cmd_res) == 0:
cmd_res = 'cmd has no output...'
conn.send(str(len(cmd_res)).encode('utf-8')) #先给客户端发送文件大小
#time.sleep(0.5)
client_ack = conn.recv(1024) #检测客户端是否接收到了
print("ack from client:", client_ack)
conn.send(cmd_res.encode('utf-8'))
server.close()
客户端代码:
# Author:dancheng
import socket
client = socket.socket()
client.connect(('localhost', 9999))
while True:
cmd = input(">>:").strip()
if len(cmd) == 0:continue
client.send(cmd.encode('utf-8'))
cmd_res_size = client.recv(1024) #接收命令结果的长度
print("命令长度:",cmd_res_size)
client.send("准备好接收了,loser可以发了".encode('utf-8'))
received_size = 0
received_data = b''
while received_size < int(cmd_res_size.decode()):
data = client.recv(1024)
received_size += len(data) #每次收到的有可能小于1024,所以必须用len判断
received_data += data
else:
print("cmd res receive done...", received_size)
print(received_data.decode())
client.close()