day25
内容详细
1.TCP和UDP
-
TCP (Transmission Control Protocol)可靠的、面向连接的协议(eg:打电话)、传输效率低全双工通信(发送缓存&接收缓存)、面向字节流。使用TCP的应用:web浏览器;电子邮件、文件传输程序。
-
UDP(User Datagram Prolocol)不可靠的,无连接的服务,传输效率高(发送前时小),一对一,一对多,多对一,多对多,面向报文,静最大努力服务,无堵塞控制。使用UDP的应用:域名系统(DNS);视频流;IP语音。
2.subprocess
import subprocess
res=subprocess.Popen("dir",#指定命令名
shell=True,
stderr=subprocess.PIPE,
#错误输出
stdout=subprocess.PIPE,)
#接收系统返回的值
print(res.stdout.read().decode("gbk"))
#输出接收到的值以“gbk”
3.struct
import struct
#i模式把123456压缩,四个字节,如果大于就报错
header_pack=struct.pack("i",123456)#压包
print(header_pack)
obj = struct.unpack("i",beader_pack)#解包
print(obj)
实例
客户端
import socket
import struct
sk = socket.socket()
sk.connect(('127.0.0.1',8008))
while 1:
cmd = input("请输入命令:")
sk.send(cmd.encode('utf-8')) # 字节
if cmd=="":
continue
if cmd == 'exit':
break
# header_pack=sk.recv(4)
# data_length=struct.unpack("i",header_pack)[0]
# print("data_length",data_length)
'''
b'xxx/xxx/xxx/bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
'''
data_length=int(sk.recv(1024).decode("utf8"))
print("data_length",data_length)
recv_data_length=0
recv_data=b""
while recv_data_length<data_length:
data=sk.recv(1024)
recv_data_length+=len(data)
recv_data+=data
print(recv_data.decode("gbk"))
sk.close()
服务端
# by luffycity.com
import socket
import subprocess
server = socket.socket()
server.bind(('127.0.0.1',8008))
server.listen(5)
while True:
print("server is working.....")
conn,addr = server.accept()
# 字节类型
while True:
# 针对window系统
try:
cmd = conn.recv(1024).decode("utf8") # 阻塞
if cmd == b'exit':
break
res=subprocess.Popen(cmd,
shell=True,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
)
# print("stdout",res.stdout.read())
# print("stderr",res.stderr.read().decode("gbk"))
out=res.stdout.read()
err=res.stderr.read()
print("out响应长度",len(out))
print("err响应长度",len(err))
if err:
import struct
header_pack = struct.pack("i", len(err))
conn.send(header_pack)
conn.send(err)
else:
#构建报头
import struct
header_pack=struct.pack("i",len(out))
print("header_pack",header_pack)
# # 发送报头
conn.send(str(len(out)).encode("utf8"))
# 发送数据
conn.send(out)
except Exception as e:
break
conn.close()
day25
内容详细
1.TCP和UDP
-
TCP (Transmission Control Protocol)可靠的、面向连接的协议(eg:打电话)、传输效率低全双工通信(发送缓存&接收缓存)、面向字节流。使用TCP的应用:web浏览器;电子邮件、文件传输程序。
-
UDP(User Datagram Prolocol)不可靠的,无连接的服务,传输效率高(发送前时小),一对一,一对多,多对一,多对多,面向报文,静最大努力服务,无堵塞控制。使用UDP的应用:域名系统(DNS);视频流;IP语音。
2.subprocess
import subprocess
res=subprocess.Popen("dir",#指定命令名
shell=True,
stderr=subprocess.PIPE,
#错误输出
stdout=subprocess.PIPE,)
#接收系统返回的值
print(res.stdout.read().decode("gbk"))
#输出接收到的值以“gbk”
3.struct
import struct
#i模式把123456压缩,四个字节,如果大于就报错
header_pack=struct.pack("i",123456)#压包
print(header_pack)
obj = struct.unpack("i",beader_pack)#解包
print(obj)
实例
客户端
import socket
import struct
sk = socket.socket()
sk.connect(('127.0.0.1',8008))
while 1:
cmd = input("请输入命令:")
sk.send(cmd.encode('utf-8')) # 字节
if cmd=="":
continue
if cmd == 'exit':
break
# header_pack=sk.recv(4)
# data_length=struct.unpack("i",header_pack)[0]
# print("data_length",data_length)
'''
b'xxx/xxx/xxx/bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
'''
data_length=int(sk.recv(1024).decode("utf8"))
print("data_length",data_length)
recv_data_length=0
recv_data=b""
while recv_data_length<data_length:
data=sk.recv(1024)
recv_data_length+=len(data)
recv_data+=data
print(recv_data.decode("gbk"))
sk.close()
服务端
# by luffycity.com
import socket
import subprocess
server = socket.socket()
server.bind(('127.0.0.1',8008))
server.listen(5)
while True:
print("server is working.....")
conn,addr = server.accept()
# 字节类型
while True:
# 针对window系统
try:
cmd = conn.recv(1024).decode("utf8") # 阻塞
if cmd == b'exit':
break
res=subprocess.Popen(cmd,
shell=True,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
)
# print("stdout",res.stdout.read())
# print("stderr",res.stderr.read().decode("gbk"))
out=res.stdout.read()
err=res.stderr.read()
print("out响应长度",len(out))
print("err响应长度",len(err))
if err:
import struct
header_pack = struct.pack("i", len(err))
conn.send(header_pack)
conn.send(err)
else:
#构建报头
import struct
header_pack=struct.pack("i",len(out))
print("header_pack",header_pack)
# # 发送报头
conn.send(str(len(out)).encode("utf8"))
# 发送数据
conn.send(out)
except Exception as e:
break
conn.close()