0
客戶端代碼寫入文本文件:如何讓我的代碼,在蟒蛇服務器
import socket
s = socket.socket()
host = '127.0.0.1'
port = 8081
s.connect((host, port))
s.send("Hello server!".encode('utf-8'))
with open('received_file.txt', 'w+') as f:
print('file opened')
while True:
print('receiving data...')
data = s.recv(1024)
print('data=%s' % data)
if not data:
break
else:
f = open('received_file.txt')
f.write(data)
f.close()
print('Successfully get the file')
s.close()
print('connection closed')
我得到以下錯誤:
TypeError: write() argument must be str, not bytes
任何答案,將不勝感激。