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
猜你喜欢
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01