我在python套接字编程中创建了一个简单的服务器客户端代码。客户端获取其侧面的屏幕截图(图像)并将其发送到服务器。当我使用'localhost'即从一个文件夹传输图像到另一个文件夹时,它工作正常.....但当图像传输到另一台计算机时出现主要问题...服务器端收到的图像已损坏...更多我观察到客户端(发送方侧图像,即未损坏的图像)和服务器(接收方侧图像,即损坏的图像)之间的差异每次几乎是1Kb .........
我的客户端(发件人)代码是 -
os.system('scrot screen.bmp') #command to take screen shot
FILE = "screen.bmp"
f = open(FILE, "rb")
data = f.read()
f.close()
del f
imagesize = int(os.path.getsize('screen.bmp'))
sendsize = '%1024s' %imagesize
s.sendall(str(sendsize))
print 'length of data = ',len(data)
s.sendall(str(len(data)))
s.sendall(str(data))
和服务器端(接收方)---
filename='screen.bmp'
print '[Media] Starting media transfer for ',filename
os.system('rm -f screen.bmp')
f = open(filename,"wb")
expsizeimage = int(conn.recv(1024))
data1 = conn.recv(1024)
data2=''
for i in range(0,len(data1)):
if(not(data1[i]=='0' or data1[i]=='1' or data1[i]=='2' or data1[i]=='3' or data1[i]=='4' or data1[i]=='5' or data1[i]=='6' or data1[i]=='7' or data1[i]=='8' or data1[i]=='9')):
break
data2=data2+data1[i]
print '------------'+data2+'-------------'+str(m)+'----------------'
print 'size of data:' ,int(data2)
print 'the expected size of image is: ', expsizeimage
data=9
del data
sized=0;
while 1:
data = conn.recv(expsizeimage)
print 'received length of image = ',len(data)
f.write(data)
sized=sized+len(data)
print "sized------"+str(sized)
del data
if(sized>=int(data2)):
break
print "saved the screentshot data recieved"
答案 0 :(得分:0)
这两个程序未经测试,是为最新版本的Python编写的,并且出于安全目的切换客户端/服务器关系。
<强> Source.py 强>
import os, struct, socket
def main():
# Take screenshot and load the data.
os.system('scrot image.bmp')
with open('image.bmp', 'rb') as file:
data = file.read()
# Construct message with data size.
size = struct.pack('!I', len(data))
message = size + data
# Open up a server socket.
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('', 65000))
server.listen(5)
# Constantly server incoming clients.
while True:
client, address = server.accept()
print('Sending data to:', address)
# Send the data and shutdown properly.
client.sendall(message)
client.shutdown(socket.SHUT_RDWR)
client.close()
if __name__ == '__main__':
main()
<强> Destination.py 强>
import socket, struct
def main(host):
# Connect to server and get image size.
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((host, 65000))
packed = recvall(client, struct.calcsize('!I'))
# Decode the size and get the image data.
size = struct.unpack('!I', packed)[0]
print('Receiving data from:', host)
data = recvall(client, size)
# Shutdown the socket and create the image file.
client.shutdown(socket.SHUT_RDWR)
client.close()
with open('image.bmp', 'wb') as file:
file.write(data)
def recvall(sock, size):
message = bytearray()
# Loop until all expected data is received.
while len(message) < size:
buffer = sock.recv(size - len(message))
if not buffer:
# End of stream was found when unexpected.
raise EOFError('Could not receive all expected data!')
message.extend(buffer)
return bytes(message)
if __name__ == '__main__':
main('localhost')