I'm trying to do a python script that checks if file and/or directory has been accessed as a honeypot to alert me someone accessing areas of an Ubuntu server they shouldn't. Unfortunately the code below doesn't seem to work. During testing I was able to cat and copy the file without the timestamp changing (also tried st_mtime and st_ctime with no luck). Suggestions?
import os
alert_time = os.stat('temp.txt').st_atime
print(datetime.fromtimestamp(alert_time))
Related
Trying to download some protein data from PDB using Biopython's Bio.PDB.PDBList
Here is a min. reproducible example:
from Bio.PDB import PDBList
pdbl=PDBList()
pdbl.retrieve_pdb_file('1GAV', file_format="pdb")
This returns:
Downloading PDB structure '1GAV'...
Desired structure doesn't exists
Desired behavior is download of the PDB file to the working directory.
Possibly useful info:
Using python 3
Do not want to download whole PDB, just pick and choose files
Using a proxy, but I don't think that's the problem because Biopython uses urllib to make requests and I tried using urllib with my proxy settings and it worked fine.
I've tried for a few different PDB code/IDs and for other file types ("mmCif", "bundle") and it returns the same thing
No error is being hit, it just can't find the file in PDB apparently?
The folder where the file should appear does get made in the working directory, but the folder is empty
We think the problem has to do with our corporate VPN because it works when the VPN is off (although proxy still on).
So as sammam said, no problems in the code.
Don't know the specifics of why this occurs with our VPN, will update if I find out.
I'm getting the same error message, which I looked at the source code and found out that the "Desired structure doesn't exists" message outputs whenever an IOError is hit.
I have a Python file called caller.py located at C:\Temp. I have two other Python files: local_testlib.py located in C:\Temp and testlib.py located in C:\Temp\MyLibs.
I am trying to import both of those files in Wing IDE Pro.
import sys
sys.path.append(r'C:\Temp\MyLibs')
import testlib #located in C:\Temp\MyLibs
import local_testlib #located in C:\Temp
#check suggestions by Wing
local_testlib. #get suggestions as list of variables etc. from the file
testlib. #don't get any suggestions
print testlib.myvar #get variable value printed OK
I get suggestions only for the local_testlib, nothing for the testlib (see picture below). I do get access to the variables in the testlib.py (so it is imported correctly) though. What adjustments should I have done to make this work?
To solve this you can add C:\Temp\MyLibs to your Python Path in Wing's Project Properties (or Configure Python dialog in Wing 101). Or in Wing Personal or Pro you can set the file as the main debug file, although I just noticed that you'll need to restart Wing before that approach works due to apparent failure to rescan the file.
This could probably be changed to take local path modifications into account in our source analysis, although in general modifying sys.path like this is not a great way to do things since the added path may screw up other modules you import later if they are trying to import a module called testlib from another location. That's why we only look for modifications like this in the main debug file.
You may want to instead make MyLibs into a package by adding a file called init.py inside it and then you can do this:
from MyLibs import testlib
That solves it w/o any extra configuration in Wing IDE.
I have an app that calls upon the extension found here. I have the .py file in my /var/www folder so that it can be imported in my python code.
So, I keep getting this error:
File does not exist: /var/www/flask_util.js
in my apache error logs. It looks like, because of the name or something, it wants to find a javascript file. But, it's in python. Here's the line of code in python that import it:
from flask_util_js import FlaskUtilJs
I've tried just changing the name of the file to flask_util.js, but again, nothing. Not entirely sure what is going on here, but I am sure that I have a file in /var/www that it should be reading.
EDIT
I think, actually that the import error is coming from importing it into my HTML when I do this:
{{flask_util_js.js}}
So, what I tried was copy out the JS code from the python and create a new file with it in the correct path. When I did that, I still got the same error on the webpage, however the apache logs don't say anything (which is weird right?). So, it still doesn't work, and I don't know why
So, it's ugly but what I ended up doing was copying over the generated JS file that I could find on my server (didn't actually exist as a document in my repository). Then, apache could find it.
This part is for anyone who is actually using flask_util_js:
However, it wasn't reading in the correct Javascript for the url_mapping so I had to go in a hard-code the correct URLs. Not very scalable, but oh well.
I am trying to be able to dynamically run python code, with variables being able to be passed through to the code. I was able to do this on my computer before I added my project to the google app engine environment (because I can access all the files, but now, with google app engine, I can not do that.
I am struggling to find a solution to this problem. It does not need to be too terribly fancy, just send variables in and get html out, as well as scripts being able to be added client side (the crucial part) to whatever database method that is used.
Edit:
well basically what I mean by dynamically is so that I can import (or thats what I did in IDLE when I tested the prototype, the solution will probably not be called importing) a python script with the name of the library being stored in a variable, as well as an unknown number of variables that would be added. I got this to work on Idle, but now I need to get it to work in the google app engine environment, and people need to be able to upload scripts as well (which is the main problem that cascades into many more problems)
Edit:
When I say that I managed to get this to work on my local machine, I mean I was able to manually drop scripts into the same directory as my main script. The script would later import and execute the scripts when necessary. I was able to get this to work with the following code:
#calling function
mod = __import__('actions.'+folder+'.'+FILE)
VAR = getattr(getattr(mod, folder), FILE)
response = VAR.Main()
print response
This code worked on both my laptop and in the google app engine environment, But When I try to add more scripts to the directory is when things get problematic. On my laptop I could just move the file over one way or another because I had full access to the file directory. On Google App engine I do not have the ability to just upload a file to the same directory or subdirectory of the rest of my python scripts. So basically the problem comes up when trying to design a way to allow more code to come into the system (in my case, adding more 'plugins').
The answer is the exec statement (also known as the exec() function) or the eval() function. See http://docs.python.org/reference/simple_stmts.html#the-exec-statement and http://docs.python.org/library/functions.html?highlight=eval#eval. These can execute arbitrary Python code from a string. exec() runs a script and you get the side effects; eval() takes an expression and returns its value. Typically you pass input in as variables in the local namespace.
Ok, So what I eventually did was use the datastore to upload everything such as the name, description, uploader and code of the plugin (for now the code is just entered into a textarea box). I then, instead of importing a file located in a folder under the same directory of my code like I had before when running everything off of my desktop, Imported the plaintext code into a module using this little bit of magic:
#Initiating Variables for use by importing functions
module_name = 'mymod'
filename = 'action_file'
source = PossibleMatches[0][1] #the source code from the best matched option
# define module_name somewhere
import types
module = types.ModuleType(module_name)
# source should the code to execute
# filename should be a pseudo-filename that the code's from
# (it doesn't actually have to exist; it's used for error messages)
code_object = compile(source, filename, 'exec')
#execute the code in the context of the module
exec code_object in module.__dict__
#Executing the 'Main' Function from the code
return module.Main()
My pygtk program is an editor for XML-based documents which reference other documents, possibly online, that may in turn reference further documents.
When I load a file, the references are resolved and the documents loaded (already asynchronously). However, this process repeats every time I start the editor, so I want some local caching to save bandwidth and time for both the user and the server hosting the referenced documents.
Are there any typical ways this is done? My idea so far would be:
Get a path to a cache directory somehow (platform-independent)
Any ideas?
Put a file named md5(url) there.
If there is a cache file already existing and it's not older than $cache_policy_age take it, otherwise use HTTP (can urllib do that?) to check if it has been modified since it was downloaded.
Personnaly I use os.path.expanduser to find a good place for caches, it's quite common in unix environnement where most of the current user's config/cache is saved under his home directory, the use of a directory name starting with a dot, making an "hidden" directory.
I would do something like :
directory = os.path.join(os.path.expanduser("~"), ".my_cache")
As for the modification date of the distant file you can use urlib :
import urllib
u = urllib.urlopen("http://www.google.com")
u.info().get("last-modified")
However you should check that your HTTP server provides the last-modified HTTP header and that it is a coherent value ! (This is not always the case)