0

I am working on a simple python client and server that can write code to a file as its sent. So far I have been stuck on this error: AttributeError: 'tuple' object has no attribute 'read'

Here is the client's code:

# CCSP Client
# (C) Chris Dorman - 2013 - GPLv2

import socket
import sys

# Some settings
host = raw_input('Enter the Host: ')
port = 7700
buff = 24
connectionmax = 10

# Connect to server
server = socket.socket()
server.connect((host, port))

print 'Connected!'

while True:
    open_file = raw_input("File (include path): ")
    fcode = open(open_file, "rb")
    while True:
        readcode = fcode.read(buff)
        server.send(readcode)
        if not fcode:
            server.send("OK\n")
            print "Transfer complete"
            break

Server:

# CCSP Server
# (C) Chris Dorman - 2013 - GPLv2

import socket
import sys
import string
import random

def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
    return ''.join(random.choice(chars) for x in range(size))

host = "0.0.0.0"
port = 7700
buff = 1024
filepath = "/home/chris/"
extension = ".txt"

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((host, port))

print "Server Started"


while True:
    server.listen(1)
    conn = server.accept()  
    print 'Client' + str(conn)
    print 'Generating a random file'
    filename = filepath + str(id_generator()) + extension
    fcode = open(filename, "wb")
    while True:
        if conn != 0: 
            code = conn.read(buff)
            fcode.write(buff)
            if conn == "DONE": 
                print 'Transfer complete'
                break #EOT

Any help with getting this to work would be awesome. I just keep getting that dumb error when it gets down to: code = conn.read(buff) on the servers script

1
  • 1
    socket.accept returns a tuple… try help(socket). Commented Dec 21, 2013 at 0:21

2 Answers 2

2

You should read some doc. accept() returns a tuple not a file-like object.

Sign up to request clarification or add additional context in comments.

Comments

0

As others have pointed out, accept() returns a tuple. It looks like you want the first item in the tuple, which will be a new socket object.

Of course, sockets don't have a read() method either. I'm guessing that what you actually want is:

code = conn.recv(buff)

As recv() returns data that has been written to a socket's connection.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.