Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
192 views
in Technique[技术] by (71.8m points)

Python - AttributeError: 'tuple' object has no attribute 'read'

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
")
            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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...