Python 套接字服务器,限制接收的数据
Python socket server, limiting data received
当尝试从我网络上的一台设备向另一台设备发送带套接字的文件时,它会将数据限制为 2760 字节,并且无法在服务器端接收到整个文件
这是我 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
这是我的客户端在另一台设备上的代码
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
当我取消 try 循环时,它没有给出任何错误或原因来说明为什么我只接收文件的一部分(2760 字节)。它还会随机(很少)发送超过 2760 个字节,或者有时会发送所有字节。不过,它始终只发送 2760 个字节。
也许您想使用 sendall()
而不是 send()
。基本上 sendall()
保证所有数据都将被发送,除非引发异常,这是其他高级语言也提供的 python 功能。
为了接收所有发送的字节,最好使用循环来完成:
data = bytearray(1)
# loop until there is no more data to receive.
while data:
data = socket.recv(1024) # Receive 1024 bytes at a time.
关于 send()
和 sendall()
之间的区别在 python .
中有一个很好解释的答案
当尝试从我网络上的一台设备向另一台设备发送带套接字的文件时,它会将数据限制为 2760 字节,并且无法在服务器端接收到整个文件
这是我 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
这是我的客户端在另一台设备上的代码
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
当我取消 try 循环时,它没有给出任何错误或原因来说明为什么我只接收文件的一部分(2760 字节)。它还会随机(很少)发送超过 2760 个字节,或者有时会发送所有字节。不过,它始终只发送 2760 个字节。
也许您想使用 sendall()
而不是 send()
。基本上 sendall()
保证所有数据都将被发送,除非引发异常,这是其他高级语言也提供的 python 功能。
为了接收所有发送的字节,最好使用循环来完成:
data = bytearray(1)
# loop until there is no more data to receive.
while data:
data = socket.recv(1024) # Receive 1024 bytes at a time.
关于 send()
和 sendall()
之间的区别在 python