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
基础教程推荐
猜你喜欢
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 筛选NumPy数组 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01