gpt4 book ai didi

python - 糟糕,未处理的类型 3 ('unimplemented' ) 将 SFTP 与 Paramiko 连接时

转载 作者:太空宇宙 更新时间:2023-11-04 01:52:33 31 4
gpt4 key购买 nike

我试图让下面的脚本工作,以便从远程服务器(服务器 1)读取特定目录中的 CSV 文件列表,并将数据移植到另一台服务器的 PostgreSQL 数据库中。

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

我已经尝试将 rsa 公钥/私钥复制到一个文件夹(.ssh 文件夹在我运行 PY 脚本时没有占用)并给出了它的位置,甚至尝试粘贴整个私钥但它不起作用并且在下面引发异常。

我在“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 属性的 RSA key ,问题已经解决。

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

关于python - 糟糕,未处理的类型 3 ('unimplemented' ) 将 SFTP 与 Paramiko 连接时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57691213/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com