简体   繁体   English

Python 套接字服务器,限制接收数据

[英]Python socket server, limiting data received

When trying to send a file with sockets from one device on my.network to another it is throttling the data to 2760 bytes and not receiving the whole file on the server side当尝试将 sockets 的文件从 my.network 上的一台设备发送到另一台设备时,它会将数据限制为 2760 字节,并且不会在服务器端接收到整个文件

Here is the code of my server on my raspberry pi:这是我在树莓派上的服务器代码:

import socket
import os

host = "my_ip"
port = "my_port"

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen()

def send(mess):
    con.send(bytes(mess, "utf-8"))

def receive():
    global message
    message = con.recv(22000).decode('utf-8')

while True:
    try:
        con, address = s.accept()
        print(f"Connection to {address} made successfully")
        receive()
        if message == "quit":
            break
        else:
            send("received")
            print(len(message))
            with open("file.py", 'w', encoding='utf-8')as a:
                a.write(message)
            os.system("python3 file.py")
    except:
        pass

And here is the code of my client on another device这是我的客户在另一台设备上的代码

with open('file.py', 'r', encoding = 'utf-8')as f:
    python_file = f.read()

print(len(python_file))
#returns 20940
import socket

host = "my_ip"
port = my_port

def send(mess):
    s.send(bytes(mess, 'utf-8'))
    
def receive():
    message = s.recv(1024).decode('utf-8')
    print(message)
    
while True:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    send_it = input("Send File?[y/n]: ")
    if send_it == "y":
        try:
            s.connect((host, port))
            send(python_file)
            recieve()
        except:
            pass
    if send_it == "quit":
        try:
            s.connect((host, port))
            send(send_it)
            recieve()
        except:
            pass
    else:
        pass

When i take off the try loop it doesn't give any errors or reason as to why im only receiving part (2760 bytes) of the file.当我取消 try 循环时,它不会给出任何错误或原因,说明为什么我只接收文件的一部分(2760 字节)。 Also it will randomly(rarely) send more than 2760 bytes or it will sometimes send all the bytes.它还会随机(很少)发送超过 2760 个字节,或者有时会发送所有字节。 Its pretty consistently only sending 2760 bytes though.不过,它始终只发送 2760 个字节。

Maybe you want to use sendall() instead of send() .也许您想使用sendall()而不是send() Basically sendall() guarantees that all data will be sent unless an exception is raised and it's a python feature that is offered by other high level languages as well.基本上sendall()保证将发送所有数据,除非引发异常,并且它是其他高级语言也提供的 python 功能。

In order to receive all bytes sent, it's a good idea to do that using a loop:为了接收发送的所有字节,最好使用循环来执行此操作:

data = bytearray(1)
# loop until there is no more data to receive.
while data:
   data = socket.recv(1024) # Receive 1024 bytes at a time.

There is a well explained answer on what the difference between send() and sendall() is in python here .关于send()sendall()之间的区别在 python here中有一个很好解释的答案。

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

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