Error 13 Permission Denied pysftp library - python-3.x

Error 13 Permission Error to a folder.
I am trying to write a code to upload a document to sftp host using python.
I am getting the following error [Error 13] Permission Denied. Can you assist, please? I am using Windows 10 Operating System
import pysftp
hostname = "host.com"
username = "username"
password = "password"
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
with pysftp.Connection(host = hostname, username = username, password = password, cnopts=cnopts) as sftp:
remoteFilePath = r'\Home\FolderName\file.txt'
localFilePath = r'C:\Users\user\Desktop\file.txt'
sftp.put(localFilePath,remoteFilePath)
Here's the error that I am getting:
File "<pyshell#23>", line 5, in <module>
sftp.put(localFilePath,remoteFilePath)
File "C:\Users\user\\AppData\Roaming\Python\Python39\site-packages\pysftp\__init__.py", line 363, in put
sftpattrs = self._sftp.put(localpath, remotepath, callback=callback,
File "C:\Users\user\AppData\Roaming\Python\Python39\site-packages\paramiko\sftp_client.py", line 759, in put
return self.putfo(fl, remotepath, file_size, callback, confirm)
File "C:\Users\user\AppData\Roaming\Python\Python39\site-packages\paramiko\sftp_client.py", line 714, in putfo
with self.file(remotepath, "wb") as fr:
File "C:\Users\user\AppData\Roaming\Python\Python39\site-packages\paramiko\sftp_client.py", line 372, in open
t, msg = self._request(CMD_OPEN, filename, imode, attrblock)
File "C:\Users\user\AppData\Roaming\Python\Python39\site-packages\paramiko\sftp_client.py", line 813, in _request
return self._read_response(num)
File "C:\Users\user\AppData\Roaming\Python\Python39\site-packages\paramiko\sftp_client.py", line 865, in _read_response
self._convert_status(msg)
File "C:\Users\user\AppData\Roaming\Python\Python39\site-packages\paramiko\sftp_client.py", line 896, in _convert_status
raise IOError(errno.EACCES, text)
PermissionError: [Errno 13] Permission denied

Related

OSError: [Errno 99] Address not available - sending e-mail from django app with gmail smtp

I am trying to send the email using smtp with django application. It is working on my local environment, but not working on production environment.
File "/usr/local/lib/python3.9/site-packages/django/core/mail/message.py", line 298, in send
return self.get_connection(fail_silently).send_messages([self])
File "/usr/local/lib/python3.9/site-packages/django/core/mail/backends/smtp.py", line 124, in send_messages
new_conn_created = self.open()
File "/usr/local/lib/python3.9/site-packages/django/core/mail/backends/smtp.py", line 80, in open
self.connection = self.connection_class(
File "/usr/local/lib/python3.9/smtplib.py", line 255, in __init__
(code, msg) = self.connect(host, port)
File "/usr/local/lib/python3.9/smtplib.py", line 341, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/local/lib/python3.9/smtplib.py", line 312, in _get_socket
return socket.create_connection((host, port), timeout,
File "/usr/local/lib/python3.9/socket.py", line 844, in create_connection
raise err
File "/usr/local/lib/python3.9/socket.py", line 832, in create_connection
sock.connect(sa)
OSError: [Errno 99] Address not available
My settings files has credentials as below:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_PORT = 587
DEFAULT_FROM_EMAIL = '********'
EMAIL_HOST_USER = '*******'
EMAIL_HOST_PASSWORD = '******'
Tried with different credentials, but getting the same error. I tried to print the email credentials from django setting and it is correct.

How can i connect this hello-flask-app with my database and create model in the database

I got this error trying to create a model for my database using fask_sqlalchemy
Traceback (most recent call last): File
"/usr/lib/python3/dist-packages/sqlalchemy/engine/base.py", line 3250,
in _wrap_pool_connect
return fn() File "/usr/lib/python3/dist-packages/sqlalchemy/pool/base.py", line 310, in
connect
return _ConnectionFairy._checkout(self) File "/usr/lib/python3/dist-packages/sqlalchemy/pool/base.py", line 868, in
_checkout
fairy = _ConnectionRecord.checkout(pool) File "/usr/lib/python3/dist-packages/sqlalchemy/pool/base.py", line 476, in
checkout
rec = pool.do_get() File "/usr/lib/python3/dist-packages/sqlalchemy/pool/impl.py", line 145, in
do_get
with util.safe_reraise(): File "/usr/lib/python3/dist-packages/sqlalchemy/util/langhelpers.py", line
70, in exit
compat.raise( File "/usr/lib/python3/dist-packages/sqlalchemy/util/compat.py", line 207,
in raise
raise exception File "/usr/lib/python3/dist-packages/sqlalchemy/pool/impl.py", line 143, in
_do_get
return self._create_connection() File "/usr/lib/python3/dist-packages/sqlalchemy/pool/base.py", line 256, in
_create_connection
return _ConnectionRecord(self) File "/usr/lib/python3/dist-packages/sqlalchemy/pool/base.py", line 371, in
init
self.__connect() File "/usr/lib/python3/dist-packages/sqlalchemy/pool/base.py", line 665, in
connect
with util.safe_reraise(): File "/usr/lib/python3/dist-packages/sqlalchemy/util/langhelpers.py", line
70, in exit
compat.raise( File "/usr/lib/python3/dist-packages/sqlalchemy/util/compat.py", line 207,
in raise
raise exception File "/usr/lib/python3/dist-packages/sqlalchemy/pool/base.py", line 661, in
__connect
self.dbapi_connection = connection = pool._invoke_creator(self) File "/usr/lib/python3/dist-packages/sqlalchemy/engine/create.py",
line 590, in connect
return dialect.connect(*cargs, **cparams) File "/usr/lib/python3/dist-packages/sqlalchemy/engine/default.py", line
597, in connect
return self.dbapi.connect(*cargs, **cparams) File "/home/mikado/.local/lib/python3.10/site-packages/psycopg2/init.py",
line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync) psycopg2.OperationalError: connection to server at "127.0.0.1", port 5432 failed: fe_sendauth: no password supplied
My Code:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://mikado:#127.0.0.1:5432/hellodb'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class Person(db.Model):
__tablename__= 'persons'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(), nullable=False)
db.create_all()
#app.route('/')
def index():
return 'Hello World!'
if __name__ == '__main__':
app.run(host="0.0.0.0")
Based on the error, you haven’t supplied any credentials for SQL:
no password supplied
You’ll need to use connection strings to pass the password as well as explained on https://flask-sqlalchemy.palletsprojects.com/en/2.x/config.html. Amending your existing connection string, replace the <password here> section with the relevant information:
postgresql+psycopg2://mikado:<password here>#127.0.0.1:5432/hellodb

Connecting to Splunk using python failing with error ConnectionResetError: [Errno 104] Connection reset by peer

I am trying to connect to Splunk using below python code . But failing with error ConnectionResetError: [Errno 104] Connection reset by peer
import urllib
import httplib2
username = '*********'
password = '******'
baseurl = 'https://xyz.splunkcloud.com:8089'
myhttp = httplib2.Http(disable_ssl_certificate_validation=True)
myhttp.add_credentials(username, password)
servercontent = myhttp.request(baseurl + '/services/auth/login', 'POST', headers={}, body=urllib.parse.urlencode({'username':username, 'password':password}))[1]
Error message:
Traceback (most recent call last):
File "splunktest.py", line 8, in <module>
servercontent = myhttp.request(baseurl + '/services/auth/login', 'POST', headers={}, body=urllib.parse.urlencode({'username':username, 'password':password}))[1]
File "/usr/lib/python3/dist-packages/httplib2/__init__.py", line 1316, in request
(response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
File "/usr/lib/python3/dist-packages/httplib2/__init__.py", line 1066, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, headers)
File "/usr/lib/python3/dist-packages/httplib2/__init__.py", line 1019, in _conn_request
response = conn.getresponse()
File "/usr/lib/python3.8/http/client.py", line 1322, in getresponse
response.begin()
File "/usr/lib/python3.8/http/client.py", line 303, in begin
version, status, reason = self._read_status()
File "/usr/lib/python3.8/http/client.py", line 264, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "/usr/lib/python3.8/socket.py", line 669, in readinto
return self._sock.recv_into(b)
ConnectionResetError: [Errno 104] Connection reset by peer
But I am able to connect to splunk using Curl command below
curl -k -u ********:****** https://xyz.splunkcloud.com:8089/services/messages
Please let me know fix for this issue

SSL Error when sending email with SMTPLib

I am writing a small Python program that involves sending emails to players in a game. I'm currently using SMTPlib to do so, and for the first while it worked great, however now I get this error whenever I try to run it:
Traceback (most recent call last):
File "/Users/user/Desktop/test.py", line 17, in <module>
sendEmail()
File "/Users/user/Desktop/test.py", line 11, in sendEmail
with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 1034, in __init__
SMTP.__init__(self, host, port, local_hostname, timeout,
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 253, in __init__
(code, msg) = self.connect(host, port)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 339, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 1041, in _get_socket
new_socket = self.context.wrap_socket(new_socket,
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 500, in wrap_socket
return self.sslsocket_class._create(
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 1040, in _create
self.do_handshake()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 1309, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1122)
And the bare-bones code that throws this error:
import smtplib, ssl
def sendEmail():
port = 465 # For SSL
password = 'password'
senderEmail = 'senderEmail#email.com'
# Create a secure SSL context
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
server.login(senderEmail, password)
message = 'Hello world!'
server.sendmail(senderEmail, 'myEmail#email.com', message)
sendEmail()
I suspect that this error has to do with me using Python 3.9.0 instead of 3.7.6 as that is the only thing that has changed about my environment since the last time it was working, but I'm not certain about that*. What is causing this error, and what can I do to fix it?
*EDIT: I ran the same code on Python 3.7.6 in case that was the problem. Interestingly, it now gives a very similar error:
Traceback (most recent call last):
File "test.py", line 17, in <module>
sendEmail()
File "test.py", line 11, in sendEmail
with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 1031, in __init__
source_address)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 1039, in _get_socket
server_hostname=self._host)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 423, in wrap_socket
session=session
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 870, in _create
self.do_handshake()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 1139, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1076)

Add proxy credential to google-calendar connection

Im trying to get event from google calendar by python, Im using google quickstart example, I get browser opening to confirm credential but on my pc I get following error:
Traceback (most recent call last):
File "C:\Programs\Python\Python35-32\lib\site-packages\httplib2\__init__.py", line 987, in _conn_request
conn.connect()
File "C:\Programs\Python\Python35-32\lib\http\client.py", line 1252, in connect
super().connect()
File "C:\Programs\Python\Python35-32\lib\http\client.py", line 849, in connect
(self.host,self.port), self.timeout, self.source_address)
File "C:\Programs\Python\Python35-32\lib\socket.py", line 693, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\Programs\Python\Python35-32\lib\socket.py", line 732, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11002] getaddrinfo failed
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\myUser\Documents\Python_projects\quickstart.py", line 79, in <module>
main()
File "C:\Users\myUser\Documents\Python_projects\quickstart.py", line 60, in main
credentials = get_credentials()
File "C:\Users\myUser\Documents\Python_projects\quickstart.py", line 48, in get_credentials
credentials = tools.run_flow(flow, store, flags)
File "C:\Programs\Python\Python35-32\lib\site-packages\oauth2client\util.py", line 137, in positional_wrapper
return wrapped(*args, **kwargs)
File "C:\Programs\Python\Python35-32\lib\site-packages\oauth2client\tools.py", line 243, in run_flow
credential = flow.step2_exchange(code, http=http)
File "C:\Programs\Python\Python35-32\lib\site-packages\oauth2client\util.py", line 137, in positional_wrapper
return wrapped(*args, **kwargs)
File "C:\Programs\Python\Python35-32\lib\site-packages\oauth2client\client.py", line 2027, in step2_exchange
headers=headers)
File "C:\Programs\Python\Python35-32\lib\site-packages\httplib2\__init__.py", line 1314, in request
(response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
File "C:\Programs\Python\Python35-32\lib\site-packages\httplib2\__init__.py", line 1064, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, headers)
File "C:\Programs\Python\Python35-32\lib\site-packages\httplib2\__init__.py", line 994, in _conn_request
raise ServerNotFoundError("Unable to find the server at %s" % conn.host)
httplib2.ServerNotFoundError: Unable to find the server at accounts.google.com
I suppose the problem is due to my proxy but where can I set proxy setting?
Thanks in advance

Resources