简体   繁体   English

使用 TCP 将图像文件从客户端发送到服务器的 Python 程序

[英]A program to send an image file from client to server in python using TCP

This is my code.这是我的代码。 Currently it transfers about 75% of the image file (cropped at the bottom).目前它传输了大约 75% 的图像文件(在底部裁剪)。 I want it transferred as a whole.我想把它作为一个整体转移。 This is how I've been working on it:这就是我一直在努力的方式:

  1. Create a folder in server pc called "server"在服务器 pc 中创建一个名为“服务器”的文件夹

  2. Create a folder in client pc called "client"在客户端 pc 中创建一个名为“client”的文件夹

  3. Also include the image file you want to transfer in the same "client" folder还要在同一个“客户端”文件夹中包含您要传输的图像文件

  4. Client side ask "name of the file you want to send", I type in the name of the image file in the folder客户端询问“您要发送的文件名”,我在文件夹中输入图片文件名

  5. Aerver side ask me "name of the file you want to receive", I type any random name. Aerver那边问我“你要接收的文件名”,我随便输入一个名字。

  6. The file transfers and gets saved to "server" folder in the server PC.文件传输并保存到服务器 PC 的“服务器”文件夹中。

client.py

from socket import *

def establish_connection(server_IP, server_port):
    client_socket = socket(AF_INET, SOCK_STREAM)
    client_socket.connect((server_IP, server_port))
    return client_socket

def send_recv_message(client_socket, message, buffer_size):
    file = open(filename,'rb')
    file_data = file.read(buffer_size)
    client_socket.send(file_data)
    text=print("File transfered")
    return text



server_IP = input("Please, enter server IP address: ")
server_port = input("Please, enter server TCP connection port number: ")
client_socket = establish_connection(server_IP, int(server_port))
print("Connection established\n")



filename= input(str("enter filename"))

received_message = send_recv_message(client_socket,filename,1048576)

server.py

from socket import *

def create_socket(server_port):
    server_socket = socket(AF_INET, SOCK_STREAM)
    server_socket.bind(('', server_port))
    server_socket.listen(1)
    return server_socket


def recv_message(server_socket, buffer_size):
    file = open(filename, 'wb')
    connection_socket, address = server_socket.accept()
    file_data= connection_socket.recv(buffer_size)
    file.write(file_data)
    file.close()
    text=("file recieved")
    return text


server_port = input("Please, enter TCP connection port number: ")
server_socket = create_socket(int(server_port))

while (True):
    filename=input(str("enter filename for the incoming file:"))
    incoming_message = recv_message(server_socket,  1048576)
    print ("{}" .format(incoming_message))

send may not send the entire buffer. send可能不会发送整个缓冲区。 You need to use sendall instead.您需要改用sendall

recv may not receive all the available data. recv可能无法接收到所有可用数据。 You need to call recv in a loop until the connection is closed.您需要循环调用recv直到连接关闭。

this is because the picture is more than the buffer size.这是因为图片大于缓冲区大小。 you have to send the size of the pic form client to server..example for text sending this can be used for image to...您必须将 pic 表单客户端的大小发送到服务器..例如,文本发送可用于图像...

eg例如

 send_message = input()

mess = f'{len(send_message)}'.ljust(buffer_size) + send_message soc.send(mess.encode())混乱 = f'{len(send_message)}'.ljust(buffer_size) + send_message soc.send(mess.encode())

at the server side find the size of picture and while size is not equal to your message recieved keep appending the msg.在服务器端找到图片的大小,虽然大小不等于您收到的消息,请继续附加 msg。

  message = ''
    mes_len = 0
    new_msg = True
    print('got the connection: {}'.format(self.request))
    while True:
        data = self.request.recv(buffer_size)
        if new_msg:
            mes_len = int(data.decode())
            new_msg = False
        message += data.decode()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM