TypeError: a bytes-like object is required, not '_io.TextIOWrapper'
TypeError: a bytes-like object is required, not '_io.TextIOWrapper'
我正在尝试通过网络发送文件,但这段代码给我带来了一些麻烦:
def Network_Vinfo():
Uinfo = [] # list call
host = "localhost"
port = 8080
Nvi = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Nvi.connect((host, port))
print("Connected to Server, Initializing voting procedures.")#Sever is connected
with open("ballot1.txt", "w+") as ballot:# creating the file
A =str(input("Enter your name"))#Info asking for users info to count "Vote"
b = str(input("Enter your Address"))
c =str(input("Enter your Driver License No."))
print('Candidates: John Ossoff, Raphael Warnock, David Perdue, Kelly loeffler')
d =str(input("Enter your Preferred Candidate"))
Uinfo = [A,b,c,d]
#write items in list on Newline
for U in Uinfo:
ballot.write('%s\n' % U)
data = ballot
while (data):
Nvi.sendall(ballot)
data = ballot.read(1024)
print("finished sending")
问题开始于程序试图在 while(data) 循环中发送文件。请帮帮我,我很绝望!
您必须发送从文件读取的数据,而不是文件对象。
代码的最后一部分应该是这样的:
ballot.seek(0)
data = ballot.read(1024)
while (data):
Nvi.sendall(data)
data = ballot.read(1024)
print("finished sending")
我正在尝试通过网络发送文件,但这段代码给我带来了一些麻烦:
def Network_Vinfo():
Uinfo = [] # list call
host = "localhost"
port = 8080
Nvi = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Nvi.connect((host, port))
print("Connected to Server, Initializing voting procedures.")#Sever is connected
with open("ballot1.txt", "w+") as ballot:# creating the file
A =str(input("Enter your name"))#Info asking for users info to count "Vote"
b = str(input("Enter your Address"))
c =str(input("Enter your Driver License No."))
print('Candidates: John Ossoff, Raphael Warnock, David Perdue, Kelly loeffler')
d =str(input("Enter your Preferred Candidate"))
Uinfo = [A,b,c,d]
#write items in list on Newline
for U in Uinfo:
ballot.write('%s\n' % U)
data = ballot
while (data):
Nvi.sendall(ballot)
data = ballot.read(1024)
print("finished sending")
问题开始于程序试图在 while(data) 循环中发送文件。请帮帮我,我很绝望!
您必须发送从文件读取的数据,而不是文件对象。
代码的最后一部分应该是这样的:
ballot.seek(0)
data = ballot.read(1024)
while (data):
Nvi.sendall(data)
data = ballot.read(1024)
print("finished sending")