要实现这一点,我已经编写了socket_client.py和socket_server.py。我把所有的努力都付诸实践,但我无法达到目的。 在当前代码中,我希望socket_client.py应该读取块设备数据并使用从中读取数据的最大值发送到socket_server.py。在socket程序中,我只想解压缩struct中包含的数据并在屏幕上显示。
注意:
如果有更好的方法,请告诉我。我是pyhon的新手。所以根据我的理解,我试着编写这段代码来实现结果。 需要大家的帮助。
socket_client.py:
import socket
import sys
import os,pickle
import struct as st
dev_read_data = os.open("/dev/sdc1",os.O_RDONLY)
buffer_size = 230400 #1048576
offset = 0
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server_address = ('localhost',10000)
print ('connecting to {} port {}'.format(server_address[0],server_address[1]))
sock.connect(server_address)
data_list = []
sent_data_size = 0
try:
while True:
data = os.pread(dev_read_data,buffer_size,offset)
#Data should be binary as its string
packed_data = st.pack("%dsi" %(len(data),),data,offset)
st_size = st.calcsize("%dsi" %(len(data)))
print ('st_size {}'.format(st_size))
packed_st_size = st.pack("i",st_size)
sock.send(packed_st_size)
#Sending data
while sent_data_size < st_size: #Send data till sent_data_size < st_size i.e. struct size
print ('{} size expected to recev'.format(st_size))
print ('value of sent_data_size before send command {}'.format(sent_data_size))
sock.send(packed_data)
sent_data_size = sock.recv(16) # rec the amount of data sent from socket_client.py to socket_server.py. This info is sent by socket_server.py
print ('value of sent_data_size sock.send command {}'.format(sent_data_size))
sent_data_size = st.unpack("Q",sent_data_size) #unpacking sent_data_size so that it can be compared in while loop condition
print ('value of sent_data_size sock.send command {}'.format(sent_data_size))
print ('Inside while loop')
#print ('Sent from offset {} data size {}'.format(offset,buffer_size))
print ('Outside while loop')
offset += buffer_size # Changing the offset to read new data using pread.
if offset == 10036977152:
break
finally:
print ('Closing socket')
sock.close()
socket_server.py:
import sys
import socket
import os
import pickle
import struct as st
#create a tcp/ip socket
dev_write_data = os.open("/dev/sdb1",os.O_WRONLY)
buffer_size = 230400 #1048576
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server_address = ('localhost',10000)
print ('Starting up on {} port {}'.format(server_address[0],server_address[1]))
sock.bind(server_address)
sock.listen(5)
data_size = 0
while True:
print ('waiting for a connection')
connection,client_address = sock.accept()
try:
print ('connection from {}'.format(client_address))
packed_st_size = connection.recv(16) #rec size of struct sent from socket_client.py
print ('Struct size {}'.format(packed_st_size))
st_size = st.unpack("i",packed_st_size)[0] # After unpacking data returned is tuple. So this stmt takes 0th index data from tuple and initializing to st_size
print ('Outside while loop struct size {}'.format(st_size))
print ('Outside while Data_Size {}'.format(data_size))
while data_size <= st_size: #Keep looping till rec data_size <= st_size i.e size of struct which was actually sent.
print ('Inside while loop struct size {}'.format(st_size))
print ('Outside while Data_Size {}'.format(data_size))
if data_size == 0: # if data_size is 0, data variable is initialized with data send by socket_client.py
data = connection.recv(st_size)
else:
data += connection.recv(st_size) #Otherwise byte stream data is concatinated with sequentially sent data by socket_client.py
x = len(data) #get data len.
data_len_rec = st.pack("Q",x) #get the data len rec in bytes using struct pack method
connection.send(data_len_rec) #Sending the len of data recv to socket_client.py
print ('{} bytes received.'.format(x))
data_size += len(data) # Adding length of rec data to data_size for iteration
#final_data = st.unpack("%dsi" %(len(data)),data)
#print ('Final Data recv {}'.format(final_data))
finally:
connection.close()