I am trying to load remote server certificate and save it in my local disk. This is the python script I'm using:
from M2Crypto.X509 import FORMAT_PEM
import StringIO
import traceback
from M2Crypto.Err import SSLError
import ssl
import socket
import pprint
import M2Crypto
from M2Crypto import X509, RSA
from datetime import datetime
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
context.verify_mode = ssl.CERT_NONE
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
port = 443
host='216.58.212.67' #google
#creating ssl socket
ssock = context.wrap_socket(socket.socket(socket.AF_INET), server_hostname=host)
#ssl connection
try:
ssock.connect((host, port))
except socket.error: #if tls connection is not possible
print "Faile connection with: " + host
#get the certificate
cert = ssock.getpeercert(True)
x509 = M2Crypto.X509.load_cert_der_string(cert)
x509_file= M2Crypto.X509.load_cert('C:/Users/xxx/Documents/temp',format=FORMAT_PEM)
When I run it, I get this error:
Traceback (most recent call last):
File "C:/Users/ealas/PycharmProjects/tlsScan/test.py", line 36, in <module>
x509_file= M2Crypto.X509.load_cert('C:/Users/xxx/Documents/temp',format=FORMAT_PEM)
File "C:\Python27\lib\site-packages\M2Crypto\X509.py", line 609, in load_cert
bio = BIO.openfile(file)
File "C:\Python27\lib\site-packages\M2Crypto\BIO.py", line 186, in openfile
return File(open(filename, mode))
IOError: [Errno 13] Permission denied: 'C:/Users/xxx/Documents/temp'
What is wrong in my code please?
You're specifying a folder when you should be specifying a file. According to the documentation for the M2Crypto.X509.load_cert function, you should be specifying the path to a file, not a folder:
Load certificate from file.
#type file: string
#param file: Name of file containing certificate in either DER or PEM format.
If you try and load data from or write data to a folder instead of a file, you will get a "Permission denied" error, at least on Windows. To test this, I created a folder called temp, and tried to read data from it and write data to it, and I got the exact same error as in your question:
Traceback (most recent call last):
File "test.py", line 4, in <module>
with open(r'C:\Users\Random\Documents\temp', 'w') as f:
IOError: [Errno 13] Permission denied: 'C:\\Users\\Random\\Documents\\temp'
Traceback (most recent call last):
File "test.py", line 4, in <module>
with open(r'C:\Users\Random\Documents\temp', 'r') as f:
IOError: [Errno 13] Permission denied: 'C:\\Users\\Random\\Documents\\temp'
In the future, you should look at the documentation for the functions you're using to ensure that you're passing not only the right type of variable to it, but also that the data itself is what the function expects.
Also, in your question you said you're trying to write to a file, but you're using a function which reads from a file. I would suggest going through and making sure you're doing what you think you're doing. Again, reading the documentation for the library you're using will be helpful.
Related
This stuff was ok until recently and I've searched everywhere.
I'm trying to get an image from the internet and then download it.
I'm using python on Windows installed via Anaconda. THe same error occurs on powershell as well as anaconda prompt.
My code (test.py):
from PIL import Image
import requests
from io import BytesIO
import cv2
import numpy
img_url = 'https://d.newsweek.com/en/full/520858/supermoon-moon-smartphone-photo-picture.jpg'
response = requests.get(img_url)
img = Image.open(BytesIO(response.content))
uid = 1
fname = 'test'
x_start, y_start, x_end, y_end = 0,0,10,10
crp = img.crop((x_start, y_start, x_end, y_end))
crp.save('test.jpg')
The error (when running python test.py in console):
Traceback (most recent call last):
File ".\imgtest.py", line 16, in <module>
crp.save('test.jpg')
File "C:\ProgramData\Anaconda3\lib\site-packages\PIL\Image.py", line 2148, in save
fp = builtins.open(filename, "w+b")
PermissionError: [Errno 13] Permission denied: 'test.jpg'
Things I've tried
Changing permissions of the folder that the file is in
Running command prompt as administrator
Tested creating a text file as well using f = open("demo.txt", "w"). The error this time is:
Traceback (most recent call last):
File ".\imgtest.py", line 15, in
f = open("demo.txt", "w")
PermissionError: [Errno 13] Permission denied: 'demo.txt'
From the documentation, try to pass the format as a param:
crp.save('test', "jpg")
or
crp.save('test', "JPEG")
Also from the documentation, Your IOError Raise when:
the file could not be written. The file may have been created and may contain partial data.
I am trying to create an array of .jpg files, but the compiler is not building the array
More specifically, my problem is that a public folder, whose path is defined as the object path, is not accessible by my Python compiler [Spyder]. However, the folder, and its respective files are all public and open access to everyone. What might be the reason that my computer cannot access the images?
Code 1 is an simple function to find and access the file path I want, and the Kernal results show what is failing.
Code 2 is the syntax for the isolated error in the program I am applying the open() method. Kernal results depict compiler failure.
Code 1:
import os
path = r'C:/Users/BeckerLab/Pictures/Final_Sample_Set/Right2'
try:
os.path.exists(path)
if (True):
R = open(path)
R.close()
except FileNotFoundError:
print("file does not exist")
Kernal for Code 1:
!runfile('C:/Users/BeckerLab/untitled6.py', wdir='C:/Users/BeckerLab')
Traceback (most recent call last):
File "C:\Users\BeckerLab\untitled6.py", line 8, in <module>
R = open(path)
PermissionError: [Errno 13] Permission denied: 'C:/Users/BeckerLab/Pictures/Final_Sample_Set/Right2'
Code 2:
import os
rightSamples = [open(file, 'r+') for file in os.listdir(r'C:/Users/Public/Right2')]
Kernal Results for Code 2:
!runfile('C:/Users/BeckerLab/almost.py', wdir='C:/Users/BeckerLab')
2020-04-05 12:59:28
Traceback (most recent call last):
File "C:\Users\BeckerLab\almost.py", line 46, in <module>
rightSamples = [open(file, 'r+') for file in os.listdir(r'C:/Users/Public/Right2')]
File "C:\Users\BeckerLab\almost.py", line 46, in <listcomp>
rightSamples = [open(file, 'r+') for file in os.listdir(r'C:/Users/Public/Right2')]
FileNotFoundError: [Errno 2] No such file or directory: 'R1.JPG'
Notice that your condition is:
os.path.exists(path)
if (True):
which will always be true. Maybe try:
if (os.path.exists(path)):
Try moving the files to another directory like 'D:/../BeckerLab/untitled6.py'
I'm trying to copy files from SFTP server .
I can connect using python pysftp .
I can run:
data = srv.listdir()
for i in data:
print I
And I get the Directory list. But when I try
sftp.put (localpath,"file_name.txt")
I get
>"IOError: [Errno 13] Permission denied: 'C:\\....."
I have permission to that folder, because I can run MKDIR and it creates a directory in that file path. I have tried many many different ways but no luck so far, any help is truly appreciated.
import pysftp
import os
def sftpExample():
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
with pysftp.Connection('HOST', username='username', password='Password', cnopts=cnopts) as sftp :
print 'connected '
localpath="C:\\new project\\new"
remotepath="/folder1"
sftp.put(localpath,"infso.txt")
sftp.put(localpath,remotepath)
sftp.getfo (remotepath, localpath )
srv.get_r(localpath, remotepath)
srv.close()
sftpExample()
I get this error code:
Traceback (most recent call last):
File "db_backup.py", line 42, in <module>
sftpExample()
File "db_backup.py", line 17, in sftpExample
sftp.put(localpath,"GT-Dallas SFTP infso.txt")
File "c:\Python27\lib\site-packages\pysftp\__init_.py", line 364, in put
confirm=confirm)
File "c:\Python27\lib\site-packages\paramiko\sftp_client.py", line 720, in put
with open(localpath, 'rb') as fl:
IOError: [Errno 13] Permission denied: "C:\\new project\\new"
I've tried all different ways to copy the file as you see however I've had no luck so far.
The issue is that you're trying to save a file as a directory which, at least in my experience, is what throws the Permission Denied error in pysftp.
Change this line of code:
localpath="C:\\new project\\new"
To this:
localpath="C:\\new project\\new\\infso.txt"
NOTE: infso.txt can be anything you want to name the local file being downloaded. I've used the same name here as the remote file's name from your example for symmetry and simplicity.
There are a few things that might be causing your issue, but the one that stands out to me comes from your error message:
IOError: [Errno 13] Permission denied: "C:\\new project\\new"
It might be that you need to escape the space ("\ ") or put it in a raw string r"C:\My Path With Spaces"
But in any case, I would avoid using spaces in your filenames, and you should rename your project folder to something like new_project or newproject.
Another thing that would make your life easier is if you compressed your directory into a single archive file (.tgz or .zip or something) and transfer that file.
I've been working on retrieving a file from my ftp server, the intention is to get the file, untar it on the local machine and compare the MD5 sum to the locally install package. My main focus is getting this file from the ftp server.
After running the script I get the following:
Traceback (most recent call last):
File "./tgzTest.py", line 27, in <module>
proof = tarfile.is_tarfile("test.tgz")
File "/usr/pkg/lib/python2.7/tarfile.py", line 2585, in
is_tarfile t = open(name)
File "/usr/pkg/lib/python2.7/tarfile.py", line 1660, in
open return func(name, "r", fileobj, **kwargs)
File "/usr/pkg/lib/python2.7/tarfile.py", line 1722, in
gzopen fileobj = bltn_open(name, mode + "b")
IOError: [Errno 2] No such file or directory: 'test.tgz'
The following is the code I'm currently using, thanks for any suggestions!
#!/usr/bin/python
import tarfile
import os
import ftplib
from ftplib import FTP
import hashlib
ftpServer = 'myserver.com'
password = 'null'
os.chdir("/home/user/testFolder")
ftp = FTP(ftpServer)
ftp.login('Anonymous', password)
print "You're in"
fileDir = "/pub/pkgsrc/base_pkgs"
tfile = "test.tgz"
ftp.cwd(fileDir)
print ftp.pwd()
tar = tarfile.open("test.tgz", 'r|gz')
for file in tar.getmembers():
print file.name
tar.close()
The tarfile package expects files in the local file system, not on the FTP server. You have to download the file first using the retrbinary() method of the ftp object, and pass the path to the downloaded file to tarfile.open().
I need to get file and assign the contents to line array in python.
I am using psftp but I keep getting file error, I know that the file is in there:
Here is what I have:
import pysftp
myfile="daily_data_file_122300.csv"
sftp = pysftp.Connection('192.168.101.123', username='userid', password='userpassd')
sftp.cwd('userid')
sftp.get('myfile', preserve_mtime=True)
sftp.close()
I get this error:
IOError: [Errno 2] No such file
The file is there.