Python无法打印为字符串

时间:2015-03-21 16:51:53

标签: python python-3.x

我正在尝试从文件中打印一段线但是只打印一组空的brakcets [],如何将file_data打印为字符串。

file_data=(file.readlines()[(array1):(array2)])
print(file_data)
编辑:对不起,伙计们完整的代码在这里,这只是一个与zh沟通的程序

import sys
import threading 
import paramiko
import socket

host_key=paramiko.RSAKey(filename='/home/jack/.ssh/id_rsa')

class Server(paramiko.ServerInterface):
    def __init__(self):
        self.event=threading.Event()
    def check_channel_request(self,kind,chanid):
        if kind == 'session':
            return paramiko.OPEN_SUCCEEDED
        return paramiko.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED
    def check_auth_password(self,username,password):
        if (username=='root') and (password=='toor'):
            return paramiko.AUTH_SUCCESSFUL
        return paramiko.AUTH_FAILED
try: 
    sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR, 1)
    sock.bind(('192.168.1.7', 1258))
    sock.listen(100)
    print('[+] Listening for connection....')
    client, addr=sock.accept()
except Exception as e:
    print('[-] Listen/bind/accept failed')
    sys.exit(1)
print('[+] Connection Initiated')

try:
    transport=paramiko.Transport(client)
    try:
        transport.load_server_moduli()
    except:
        print('[-] Failed to load moduli -- gex will be unsupported')
        raise
    transport.add_server_key(host_key)
    server=Server()
    try:
        transport.start_server(server=server)
    except:
        paramiko.SSHException()
        print('[-] SSH negotiation failed')
    chan=transport.accept(20)
    print('[+] Authentication Completed')
    returned=(chan.recv(1024))
    print(returned.decode(encoding='UTF-8',errors='strict'))
    chan.send("Connection established")
    while True:
        command=input('Enter Command=>').strip('\n')
        if command=='exit':
            transport.close()
            sys.exit(1)
        if command=='File_Transfer-SF':
            file_location=input('Enter File Location=>')
            file_destination=input('Enter File Destination=>')
            packet=('File_Transfer-SF-'+file_destination)
            chan.send(packet)
            chan.recv(1) 
            file=open(file_location,'r+')
            file_length=len(file.readlines())
            array1=-8
            array2=0
            print('A')
            while array2 != (file_length+1):
                array1=array1+8
                array2=array2+8
                if array2>=(file_length+1):
                    array2==(file_length+1)
                print('B')
                file_data=(file.readlines()[(array1):(array2)])
                print(print("\n".join(file_data)))
                chan.send(file_data)
                print('C')
                chan.recv(1)
                if array2==(file_length+1):
                    chan.send('**COMPLETE**')
        else:
            chan.send(command)
            print('C')
            print(chan.recv(2048).decode('UTF-8'))
except Exception as e:
    print('[-] Exception'+': ' + str(e))
    try:
       transport.close()
    except:
        pass
    sys.exit(1)

1 个答案:

答案 0 :(得分:0)

此处file_length=len(file.readlines())您完全阅读了该文件的内容,稍后您假设您可以再次阅读该内容...这不会起作用,该文件似乎是空的。您可以使用seek(0)命令重置filepointer,或者关闭并重新打开它。 就个人而言,我不会使用python关键字作为变量的名称,例如filefileopen的同义词)。只需使用' infile'或者这样。