By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,882 Members | 2,465 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,882 IT Pros & Developers. It's quick & easy.

sftp problem!

P: n/a

I want to use sftp from paramiko to copy a file from a windows machine to a
Linux in the network, I use this code :

host = "LinuxComputerName" (or its Ip)
port = 22
transport = paramiko.Transport((host, port))
password = "LinuxComputerPassword"
username = "LinuxComputerUserName"

transport.connect(username = username, password = password)
sftp = paramiko.SFTPClient.from_transport(transport)
filepath = '/home/test.jpg'
localpath = '/home/test.jpg'
sftp.get(filepath, localpath)

sftp.close()
transport.close()

and after runing this code I get this error:
socket.error: (10061, 'Connection refused')

What is wrong with this code?
please help me.

--
View this message in context: http://www.nabble.com/sftp-problem%2...p19821106.html
Sent from the Python - python-list mailing list archive at Nabble.com.

Oct 5 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
In message <ma**************************************@python.o rg>, sa6113
wrote:
host = "LinuxComputerName" (or its Ip)
port = 22

and after runing this code I get this error:
socket.error: (10061, 'Connection refused')
Did you try

sftp LinuxComputerName

just to confirm it's got a listener on port 22?
Oct 5 '08 #2

P: n/a

what do you mean?
Lawrence D'Oliveiro wrote:
>
In message <ma**************************************@python.o rg>, sa6113
wrote:
>host = "LinuxComputerName" (or its Ip)
port = 22

and after runing this code I get this error:
socket.error: (10061, 'Connection refused')

Did you try

sftp LinuxComputerName

just to confirm it's got a listener on port 22?
--
http://mail.python.org/mailman/listinfo/python-list

--
View this message in context: http://www.nabble.com/sftp-problem%2...p19823566.html
Sent from the Python - python-list mailing list archive at Nabble.com.

Oct 5 '08 #3

P: n/a
here is a snippet that works, you need to replace the default data or
create a database etc.. and the fields to store it
you also need to generate and share your public key to the receiving
server to avoid password prompt
# Standard library imports
import os
import sys
import time
import traceback
import paramiko
# my defaultdata is stored in a sqllite table called merchant.lib
# you can just assign values to defaultdata or recreate a database of
your own
from merchant.lib import defaultdata
# set up default values
hostname = defaultdata.defaults['hostname']
username = defaultdata.defaults['username']
password = defaultdata.defaults['password']

# paramiko required data
port = defaultdata.defaults['port']
fname = defaultdata.defaults['fname']
pathupload = defaultdata.defaults['pathupload']
pathdownload = defaultdata.defaults['pathdownload']
input_dir = defaultdata.defaults['input_dir']
output_dir = defaultdata.defaults['output_dir']
curdir = os.path.abspath(os.curdir)

# load ssh pkey you need to precreate the key and ensure the server
side
# has it in its authorized_keys
pkey_path = os.path.expanduser('~/.ssh/id_dsa')
pkey = paramiko.DSSKey.from_private_key_file(pkey_path)

paramiko.util.log_to_file('transfer.log')
logger = paramiko.util.get_logger('paramiko')

try:
# set up connection
t = paramiko.Transport((hostname, port))
t.connect(username=username, pkey=pkey)
sftp = paramiko.SFTPClient.from_transport(t)

# upload files
files = [file for file in os.listdir(output_dir)
if file.endswith('.txt')]
logger.info("UPLOAD FILES %s" % files)
os.chdir(output_dir)
for file in files:
sftp.put(file, os.path.join(pathupload, file))
os.rename(file, file + '.old')

# download files
response_files = [file for file in sftp.listdir(pathdownload)
if file.startswith('DISCOVER_RESPONSE_')]

logger.info("RESPOSE FILES: %s" % response_files)
os.chdir(os.path.join(curdir, input_dir))
for file in response_files:
file_path = os.path.join(pathdownload, file)
sftp.get(file_path, file)
sftp.remove(file_path)
# close connection
t.close()
Oct 6 '08 #4

P: n/a

Dear Mike,
Thanks for your help, but I am new in paramiko,would you please help me
more?

May I connect to server by password insted of private key? How?
I used this code:
....
t.auth_password(username, password, event)
....
but I received this error :
paramiko.SSHException : No existing session

I think my problem is in authentication, Do you think I have to use pycrypto
module or something like that??

If not would you please tell me how should I precreate the key and ensure
the server
side has it in its authorized_keys? How do I share that as I mentioned it
before I am using two different platforms?

Thanks.

Mike Hjorleifsson wrote:
>
here is a snippet that works, you need to replace the default data or
create a database etc.. and the fields to store it
you also need to generate and share your public key to the receiving
server to avoid password prompt
# Standard library imports
import os
import sys
import time
import traceback
import paramiko
# my defaultdata is stored in a sqllite table called merchant.lib
# you can just assign values to defaultdata or recreate a database of
your own
from merchant.lib import defaultdata
# set up default values
hostname = defaultdata.defaults['hostname']
username = defaultdata.defaults['username']
password = defaultdata.defaults['password']

# paramiko required data
port = defaultdata.defaults['port']
fname = defaultdata.defaults['fname']
pathupload = defaultdata.defaults['pathupload']
pathdownload = defaultdata.defaults['pathdownload']
input_dir = defaultdata.defaults['input_dir']
output_dir = defaultdata.defaults['output_dir']
curdir = os.path.abspath(os.curdir)

# load ssh pkey you need to precreate the key and ensure the server
side
# has it in its authorized_keys
pkey_path = os.path.expanduser('~/.ssh/id_dsa')
pkey = paramiko.DSSKey.from_private_key_file(pkey_path)

paramiko.util.log_to_file('transfer.log')
logger = paramiko.util.get_logger('paramiko')

try:
# set up connection
t = paramiko.Transport((hostname, port))
t.connect(username=username, pkey=pkey)
sftp = paramiko.SFTPClient.from_transport(t)

# upload files
files = [file for file in os.listdir(output_dir)
if file.endswith('.txt')]
logger.info("UPLOAD FILES %s" % files)
os.chdir(output_dir)
for file in files:
sftp.put(file, os.path.join(pathupload, file))
os.rename(file, file + '.old')

# download files
response_files = [file for file in sftp.listdir(pathdownload)
if file.startswith('DISCOVER_RESPONSE_')]

logger.info("RESPOSE FILES: %s" % response_files)
os.chdir(os.path.join(curdir, input_dir))
for file in response_files:
file_path = os.path.join(pathdownload, file)
sftp.get(file_path, file)
sftp.remove(file_path)
# close connection
t.close()
--
http://mail.python.org/mailman/listinfo/python-list

--
View this message in context: http://www.nabble.com/sftp-problem%2...p19833497.html
Sent from the Python - python-list mailing list archive at Nabble.com.

Oct 6 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.