Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用python将图片从服务器发送到客户端_Python_Sockets - Fatal编程技术网

如何用python将图片从服务器发送到客户端

如何用python将图片从服务器发送到客户端,python,sockets,Python,Sockets,我是python新手,我不知道我的代码有什么问题: 我设置了一个客户端和一个服务器(都在本地主机上),服务器拍摄了一个快照,并假设将其发送到保存在某个文件夹中的客户端,这一切都发生了,但JPG文件没有正确打开图片,而是说它不支持文件格式:\ 守则: # This reads all the bytes in the file print(myfile.read()) # this will read nothing since the file cursor is already at th

我是python新手,我不知道我的代码有什么问题:

我设置了一个客户端和一个服务器(都在本地主机上),服务器拍摄了一个快照,并假设将其发送到保存在某个文件夹中的客户端,这一切都发生了,但JPG文件没有正确打开图片,而是说它不支持文件格式:\

守则:

# This reads all the bytes in the file 
print(myfile.read())

# this will read nothing since the file cursor is already at the end of the file. 
client_socket.send(myfile.read())
clientFile.py

import socket

# set up a socket connection object
my_socket = socket.socket()
# make the socket connect to a certain ip&port
my_socket.connect(("127.0.0.1", 8821))

data = ''

while data != 'EXIT':
    message = input('enter a message to send to the server \n')
    # send the server what to do (in this case take a snapshot)
    my_socket.send(message.encode())
    
    data = my_socket.recv(1024).decode()
   
    if data == 'snapshot has been taken':
        # recieve the file's byte length from the server ( for example: 2000000 bytes => 7 length)
        data = my_socket.recv(1).decode()
        
        sizeLength = int(data) % 10
        # recieve the size of the file picture
        data = my_socket.recv(sizeLength).decode()
        size = int(data)

        # recieve the file in binary
        picture = my_socket.recv(size)
        # trying to write the file :\
        myfile = open(r'C:\test\client\screen.jpg', 'wb')
        myfile.write(picture)
        myfile.close()
else:
    print('closing the socket')
    my_socket.close()
server.py

    import socket
    import datetime
    import glob
    import os
    import pyautogui
    import subprocess
    
    server_socket = socket.socket()
    # accepting a socket from which client (0.0.0.0) means everyone
    server_socket.bind(("0.0.0.0", 8821))
    server_socket.listen()
    # accept sockets and recieve socket and client ip&port as tuples
    (client_socket, client_address) = server_socket.accept()
    
    # data = client_socket.recv(1024).decode()
    data = ''
    while data != 'EXIT':
    .
    .
    .
        elif data == 'SNAPSHOT':
            #snapshot saved succesfully !
            image = pyautogui.screenshot()
            image.save(r'C:\test\screen.jpg') 

            # file stats (size and stuff)
            picStats = os.stat(r'C:\test\screen.jpg')
            
            picSize = picStats.st_size
            picLength = len(str(picSize))
          
            
            myfile = open(r'C:\test\screen.jpg', 'rb')
            print(myfile.read())
            reply = 'snapshot has been taken'
            # make the client prepere for the picture to be send
            client_socket.send(reply.encode())
            # send the picture length
             client_socket.send(str(picLength).encode())
            # send the picture size 
            client_socket.send(str(picSize).encode())
            client_socket.send(myfile.read())
            
.
.
.
#closing sockets eventually ...
             
我想我错过了一些东西。。。
谢谢

我对代码有一个问题:

# This reads all the bytes in the file 
print(myfile.read())

# this will read nothing since the file cursor is already at the end of the file. 
client_socket.send(myfile.read())

将字节设置为内部对象
file\u bytes=myfile.read()
或在读取之间重置光标
myfile.seek(0)

在编辑器中打开文件(也支持以十六进制显示信息的编辑器),并查看发送文件前后的差异。特别是,我猜您对
recv
的调用包含的数据比您预期的要多,或者您使用的大小信息有误(一方面是以字符串形式发送的,另一方面是以单字节(?)形式读取的)。请编辑您问题中的代码,使其成为一个-尤其是删除这些。。。节并使其可运行。任何人都应该能够将代码复制/粘贴到两个文件中,而无需添加任何内容,运行它即可看到与您相同的结果。。您的代码不限于发送JPEG文件,您应该能够发送包含ascii内容的文件-我要做的第一件事是检查接收端是否获得了正确的文件大小。另外,您应该允许接收端接收的字节数少于完整长度,并将它们保存到文件中,然后等待剩余的字节数。您看过多处理模块了吗?这些问题都在那里解决了。你是一个传奇人物!谢谢这样愚蠢的错误