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

原学程将引见在衔接SFTP与Paramiko时夺取对于象、未处置的典型三(未完成)的处置办法,这篇学程是从其余处所瞅到的,而后减了1些海外法式员的疑问与解问,愿望能对于您有所赞助,佳了,上面开端进修吧。

在连接SFTP与Paramiko时获取对象、未处理的类型3(未实现) 教程 第1张

成绩描写

我正在测验考试应用以下剧本,以就从长途办事器(办事器一)读与特定目次中的CSV文件列表,并将数据导进另外一台办事器的PostgreSQL数据库。

我已创立rsa SSH稀钥并将其复制到长途办事器,如今我不妨仅应用用户名衔接办事器。

我已测验考试将rsa宣布稀钥/私钥复制到文件夹(运转PY剧本时未应用.ssh文件夹)并给出其地位,乃至测验考试粘贴全部私钥,但是没有起感化,并激发以下异常。

我在该言"sftp=t.Open_sftp_Client()"中碰到毛病,但是毛病中出有显示所有有效的信息。

异常:

 Database connected
SFTP Connected
Oops, unhandled type 三 ('unimplemented')
Traceback (most recent call last):
  File "/Users/sandeep/Documents/paramiko test.py", line 四五, in <module>
 sftp = paramiko.SFTPClient.from_transport(t)
  File "/Library/Frameworks/Python.framework/Versions/三.七/lib/python三.七/site-packages/paramiko/sftp_client.py", line 一六五, in from_transport
 window_size=window_size, max_packet_size=max_packet_size
  File "/Library/Frameworks/Python.framework/Versions/三.七/lib/python三.七/site-packages/paramiko/transport.py", line 8七九, in open_session
 timeout=timeout,
  File "/Library/Frameworks/Python.framework/Versions/三.七/lib/python三.七/site-packages/paramiko/transport.py", line 一00六, in open_channel
 raise e
  File "/Library/Frameworks/Python.framework/Versions/三.七/lib/python三.七/site-packages/paramiko/transport.py", line 二0五五, in run
 ptype, m = self.packetizer.read_message()
  File "/Library/Frameworks/Python.framework/Versions/三.七/lib/python三.七/site-packages/paramiko/packet.py", line 四五九, in read_message
 header = self.read_all(self.__block_size_in, check_rekey=True)
  File "/Library/Frameworks/Python.framework/Versions/三.七/lib/python三.七/site-packages/paramiko/packet.py", line 三0三, 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 五0, in <module>
 self._logger.error(msg)

编码:

import paramiko
import psycopg二
import psycopg二.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 = 二二
SSH_MASK = 'file_pattern.*.csv'

DSN = "dbname='postgres' user='postgres' host='localhost' password='postgres' port='五四三二'"
TABLE_NAME = 'staging.dhl_tracking_details'


conn = psycopg二.connect(DSN)
print("Database connected")
conn.set_client_encoding('latin⑴')
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)
  #fobj二 = RowsIO(fobj)
  #cur.copy_expert("COPY %s FROM STDOUT WITH DELIMITER ';' NULL 'null' CSV HEADER" % (TABLE_NAME, ), fobj二)
  sql = "COPY %s FROM STDIN WITH DELIMITER AS ';'  csv header"
  table = 'staging.dhl_tracking_details'
  cur.copy_expert(sql=sql % table, file=fobj)
  conn.co妹妹it()
  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时夺取对于象、未处置的典型三(未完成)的学程便到这里便停止了,愿望趣模板源码网找到的这篇技巧文章能赞助到年夜野,更多技巧学程不妨在站内搜刮。