gpt4 book ai didi

python - 下载文件后无法写入客户端

转载 作者:行者123 更新时间:2023-12-05 07:36:44 25 4
gpt4 key购买 nike

我已经能够从套接字接收文件并下载它,但是当我尝试将消息从服​​务器推送到客户端时,消息永远不会显示在客户端。

下面是代码,任何帮助将不胜感激,因为我是网络编程的新手。

# get the hostname
host = socket.gethostname()
port = 5000 # initiate port no above 1024
Buffer = 1024

server_socket = socket.socket() # get instance
# look closely. The bind() function takes tuple as argument
server_socket.bind((host, port)) # bind host address and port together


# configure how many client the server can listen simultaneously
server_socket.listen(2)
conn, address = server_socket.accept() # accept new connection
print("Connection from: " + str(address))
f = open("FileFromServer.txt", "wb")
# receive data stream. it won't accept data packet greater than 1024 bytes
data = conn.recv(Buffer)
while data:
f.write(data)
print("from connected user: " + str(data))
data = conn.recv(Buffer)

f.close()

print 'Data Recivede'
datas = 'Recived the file Thanks'
if datas is not '':
conn.send(datas) # send data to the client

conn.close() # close the connection





host = socket.gethostname() # as both code is running on same pc
port = 5000 # socket server port number

client_socket = socket.socket() # instantiate
client_socket.connect((host, port)) # connect to the server



with open('T.txt', 'rb') as f:

print 'file openedfor sending'
l = f.read(1024)
while True:
client_socket.send(l)
l = f.read(1024)

f.close()

print('Done sending')

print('receiving data...')
data = client_socket.recv(1024)
print data

client_socket.close() # close the connection
print 'conection closed

最佳答案

问题是你的服务器和客户端套接字都卡在了 while 循环中:

试试这个client.py:

import socket

host = socket.gethostname() # as both code is running on same pc
port = 5000 # socket server port number

client_socket = socket.socket() # instantiate
client_socket.connect((host, port)) # connect to the server

end = '$END MARKER$'

with open('T.txt', 'rb') as f:
print('file opened for sending')
while True:
l = f.read(1024)
if len(l + end) < 1024:
client_socket.send(l+end)
break
client_socket.send(l)

print('Done sending')

print('receiving data...')
data = client_socket.recv(1024)

print(data)

client_socket.close() # close the connection
print('conection closed')

server.py

import socket

# get the hostname
host = socket.gethostname()
port = 5000 # initiate port no above 1024
Buffer = 1024

end = '$END MARKER$'

server_socket = socket.socket() # get instance
# look closely. The bind() function takes tuple as argument
server_socket.bind((host, port)) # bind host address and port together

# configure how many client the server can listen simultaneously
server_socket.listen(2)
conn, address = server_socket.accept() # accept new connection
print("Connection from: " + str(address))
# receive data stream. it won't accept data packet greater than 1024 bytes
with open("FileFromServer.txt", "ab") as f:
while True:
data = conn.recv(Buffer)
if end in data:
f.write(data[:data.find(end)])
conn.send(b'Recived the file Thanks')
break
f.write(data)

conn.close()

关于python - 下载文件后无法写入客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49026274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com