Python文件传输错误w/ sockets

时间:2022-08-26 18:57:00

Here is my server.py:

这是我server.py:

import socket, atexit

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((socket.gethostname(), 8000))
server.listen(5)
(client,(ip,port))=server.accept()
command = raw_input('> ')

if command.rsplit(' ',1)[0] == 'write':
    client.send(command.rsplit(' ',1)[2])
    print 'Client @ ', ip + ' '
    data = client.recv(1024)
    file = open(command.rsplit(' ',1)[1],'rb')
    bytes = file.read(1024)
    while(bytes):
        client.send(bytes)
        bytes = file.read(1024)
    file.close()
    client.close()

The client.py:

client.py:

import socket

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('MY IP', 8000))
client.send("!")
name = client.recv(1024)

with open(name, 'wb') as file:
    while True:
        data = client.recv(1024)
        if not data:
            break
        file.write(data)

file.close()
client.close()

The first data transmission in server.py is supposed to send the name of the file I want to the client.py. Where it says:

服务器中的第一个数据传输。py应该将我想要的文件的名称发送给client.py。它说:

name = client.recv(1024)

in client.py, it is supposed to receive and make a file using that name. However, the server.py closes, causing the client.py to crash and not give output (host closed). If I open in IDLE to see the output, it doesn't work but nothing shows.

在客户端。py,它应该接收并使用这个名称创建一个文件。然而,服务器。py关闭,导致客户端。py崩溃而不输出(主机关闭)。如果我在IDLE中打开以查看输出,它不会工作,但什么都没有显示。

1 个解决方案

#1


1  

Your server.py needed tweaked;

你的服务器。py需要调整;

import socket, atexit

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('', 8000))
server.listen(5)
(client,(ip,port))=server.accept()
command = raw_input('> ')

if command.split(' ')[0] == 'write':
    client.send(command.split(' ')[2])
    print 'Client @ '+str(ip)+':'
    data = client.recv(1024)
    file = open(command.split(' ')[1],'rb')
    bytes = file.read(1024)
    while(bytes):
        client.send(bytes)
        bytes = file.read(1024)
    file.close()
    client.close()

The rsplit and trailing ,1's were causing the breaks.

rsplit和拖尾,1是造成断裂的原因。

Using the input write /Users/Namelessthehckr/Downloads/ucsgflmza.cs /Users/Namelessthehckr/Desktop/Test.txt, the file was successfully CP'd without error.

使用输入写/用户/Namelessthehckr/Downloads/ucsgflmza。cs /用户/ Namelessthehckr /桌面/测试。txt,文件已成功CP,没有错误。

#1


1  

Your server.py needed tweaked;

你的服务器。py需要调整;

import socket, atexit

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('', 8000))
server.listen(5)
(client,(ip,port))=server.accept()
command = raw_input('> ')

if command.split(' ')[0] == 'write':
    client.send(command.split(' ')[2])
    print 'Client @ '+str(ip)+':'
    data = client.recv(1024)
    file = open(command.split(' ')[1],'rb')
    bytes = file.read(1024)
    while(bytes):
        client.send(bytes)
        bytes = file.read(1024)
    file.close()
    client.close()

The rsplit and trailing ,1's were causing the breaks.

rsplit和拖尾,1是造成断裂的原因。

Using the input write /Users/Namelessthehckr/Downloads/ucsgflmza.cs /Users/Namelessthehckr/Desktop/Test.txt, the file was successfully CP'd without error.

使用输入写/用户/Namelessthehckr/Downloads/ucsgflmza。cs /用户/ Namelessthehckr /桌面/测试。txt,文件已成功CP,没有错误。