Python file transfer error w/ sockets

Here is my 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:

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:

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.

Answers


Your server.py needed tweaked;

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.

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


Need Your Help

PDO prepared statement 'where' issue

php mysql pdo

I'm new to PDO and I'm struggling to use the 'where' statement in a prepared statement. Below is the code I tried. It returns Data could not be retrieved from the database.

100% Height Off-Canvas Foundation 5

html css zurb-foundation

I'm using Foundation and have a series of sections at height: 100%.