在连接SFTP与Paramiko时获取对象、未处理的类型3

Getting Oops, unhandled type 3 (#39;unimplemented#39;) while connecting SFTP with Paramiko(在连接SFTP与Paramiko时获取对象、未处理的类型3(未实现))

本文介绍了在连接SFTP与Paramiko时获取对象、未处理的类型3(未实现)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下脚本,以便从远程服务器(服务器1)读取特定目录中的CSV文件列表,并将数据导入另一台服务器的PostgreSQL数据库。

我已创建rsa SSH密钥并将其复制到远程服务器,现在我可以仅使用用户名连接服务器。

我已尝试将rsa发布密钥/私钥复制到文件夹(运行PY脚本时未使用.ssh文件夹)并给出其位置,甚至尝试粘贴整个私钥,但不起作用,并引发以下异常。

我在该行"sftp=t.Open_sftp_Client()"中遇到错误,但错误中没有显示任何有用的信息。

谁能帮我一下,建议会很有帮助的。

异常:

    Database connected
SFTP Connected
Oops, unhandled type 3 ('unimplemented')
Traceback (most recent call last):
  File "/Users/sandeep/Documents/paramiko test.py", line 45, in <module>
    sftp = paramiko.SFTPClient.from_transport(t)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/paramiko/sftp_client.py", line 165, in from_transport
    window_size=window_size, max_packet_size=max_packet_size
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/paramiko/transport.py", line 879, in open_session
    timeout=timeout,
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/paramiko/transport.py", line 1006, in open_channel
    raise e
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/paramiko/transport.py", line 2055, in run
    ptype, m = self.packetizer.read_message()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/paramiko/packet.py", line 459, in read_message
    header = self.read_all(self.__block_size_in, check_rekey=True)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/paramiko/packet.py", line 303, in read_all
    raise EOFError()
EOFError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/sandeep/Documents/paramiko test.py", line 50, in <module>
    self._logger.error(msg)

编码:

import paramiko
import psycopg2
import psycopg2.extras
import os
import inspect
import re
import sys

SECTION_SRC_STAR = "DEFAULT"

SSH_HOST = '*****.com'
SSH_USERNAME = 'root'
#SSH_KEYFILE = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile(inspect.currentframe()))[0], "ssh.key")))
#SSH_KEYFILE = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile(inspect.currentframe()))[0], "id_rsa")))
SSH_KEYFILE ="/Users/sand/Documents/ssh_pub"

SSH_DIR = '/opt/prod_odoo_out/carrierfiles/dhl/'
SSH_PORT = 22
SSH_MASK = 'file_pattern.*.csv'

DSN = "dbname='postgres' user='postgres' host='localhost' password='postgres' port='5432'"
TABLE_NAME = 'staging.dhl_tracking_details'


conn = psycopg2.connect(DSN)
print("Database connected")
conn.set_client_encoding('latin-1')
cur = conn.cursor()

ssh = paramiko.SSHClient()
t = paramiko.Transport((SSH_HOST, SSH_PORT))


#key = paramiko.RSAKey.from_private_key_file(SSH_KEYFILE)
#key='***************...........'
#t.connect(username=SSH_USERNAME, pkey=key)
t.connect(username=SSH_USERNAME)
print("SFTP Connected")
try:
    #sftp = paramiko.SFTPClient.from_transport(t) 
    sftp = t.open_sftp_client()
    print("SFTP Client : Open")
except Exception as e:
    msg = "Error connecting via ssh: %s" % e
    self._logger.error(msg)
    raise paramiko.SSHException(msg)


for filename in sftp.listdir(SSH_DIR):
    if re.match(SSH_MASK, filename):
        path = '/%s/%s' % (SSH_DIR, filename)
        fobj = sftp.file(path, 'rb')
        #cur.execute('TRUNCATE TABLE %s' % TABLE_NAME)
        #fobj2 = RowsIO(fobj)
        #cur.copy_expert("COPY %s FROM STDOUT WITH DELIMITER ';' NULL 'null' CSV HEADER" % (TABLE_NAME, ), fobj2)
        sql = "COPY %s FROM STDIN WITH DELIMITER AS ';'  csv header"
        table = 'staging.dhl_tracking_details'
        cur.copy_expert(sql=sql % table, file=fobj)
        conn.commit()
        sftp.remove(path)

t.close()

推荐答案

我已经提到了具有pkey属性的RSAKey,问题已解决。

pk=paramiko.RSAKey.from_private_key(open('/Users/sandeep/Documents/ssh_pub/id_rsa'))
t.connect(username=SSH_USERNAME,pkey=pk)

这篇关于在连接SFTP与Paramiko时获取对象、未处理的类型3(未实现)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在连接SFTP与Paramiko时获取对象、未处理的类型3