I have access to apache web server. It is a web service application using Python.
I would like to access a file (100% sure reside in the local which is the web server itself) but I have only the url.
I am thinking of references any setting file of apache web server to determine the absolute local path of the file.
let say I have this kind of url: http://localhost/service/abc.jpg . So I can parse it to get the filename then obtain the decided local path from setting file.
But.. how about this kind of url: http://localhost/service/imagefile1 . In this case, I have only the url that the filename is not included in the url but still valid url. Is it possible to obtain the absolute local path of the file from url?
Any guidance is appreciated.
Is is possible to determine the local path from url?
The short answer: no.
Generally speaking, there is no concrete association between a URL and a file. In fact, there's nothing to say that the data at a given URL exists in a file on any server anywhere.
Related
I need to get the current URL of my application in my views.py. Not the bookmark I am in but the address to the home page (all).
I finded this solution:
url = "{0}://{1}".format(request.scheme, request.get_host())
But i but I think it can be simpler . Not using 'request ....' twice and by obtaining an interpretation of one variable.
Any good suggestions will be appreciated.
url = request.build_absolute_uri("/")
See the Django documentation reference:
Returns the absolute URI form of location. If no location is provided, the location will be set to request.get_full_path().
If the location is already an absolute URI, it will not be altered. Otherwise the absolute URI is built using the server variables available in this request.
This seems to be what you are asking for.
I have a django project which is running (for example) on localhost:8000.
I have an image in this address localhost:8000/static/test.jpg . A user may open just this image by going to it's url and not open the containing page.
I want to find out if this image is loaded in a user's browser (by loading the containing page or just entering the image's url) and I want to get the request object of that request.
Can I have a method in my views just for that specific image? I searched through the internet but didn't find anything useful. any idea or solution?
Are you talking about disallowing hotlinking? This can be easier - and more effectively - done with the webserver that runs in front of your Django server.
For some examples for Apache check out https://wiki.apache.org/httpd/DisableImageHotLinking
This can be only by serving your images with your custom views. E.g you should write your own view that will return static resources, and you will not use a standard django static handler
First of all, please note that serving static files in a production environment should not be handled by Django in the first place. From contrib/staticfiles/views.py:
Views and functions for serving static files. These are only to be used during
development, and SHOULD NOT be used in a production setting.
If you do want to use this, then you could write a custom middleware hooking into process_view or process_request to do your stuff in.
I did it finally. for example i have a file in localjost:8000/media/1.jpg and i want to get ip of the user who enters this url to load the 1.jpg.
I added this line in my urls :
url(r'^media/(?P<path>.*)$', 'project.views.serve','document_root': settings.MEDIA_ROOT,}), and i set the MEDIA_ROOT already. then in project.views.serve i called django.views.static.serve and i returned the result of that as a HttpResponse. i have a request argument in my project.views.serve and i did this to get the user's ip from that :
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if x_forwarded_for:
ip = x_forwarded_for.split(',')[-1].strip()
else:
ip = request.META.get('REMOTE_ADDR')
print ip
Your way goes fine but bot for a high traffic. In that case you can use XSendFile library witch works with Apache
I like to develop a simple HTTP in Python3 which basically should support 3 different behaviors:
Load local files by their relative path to the root folder e.g. myhost/static/index.html
Mirror remote URL and map them to local urls e.g. myhost/google/* => google.com/* (with HTTP/HTTPS, header support and optional local caching)
Support dynamic results on specific routes e.g. myhost/javascript/mymodule.js (compresses mymodule.js via e.g. Uglify and returns the result)
Thinking about using Tornado or CheeryPy but no luck yet regarding what's the easiest way to implement the proxy part without doing it all on my own.
Suggestions are highly appreciated.
I can't, for the life of me, get Django to find my JavaScript files! I am trying to plug in a custom widget to the admin page, like so:
class MarkItUpWidget(forms.Textarea):
class Media:
js = (
'js/jquery.js',
'js/markitup/jquery.markitup.js',
'js/markitup/sets/markdown/set.js',
'js/markitup.init.js',
)
css = {
'screen': (
'js/markitup/skins/simple/style.css',
'js/markitup/sets/markdown/style.css',
)
}
However, when this code is inserted on my admin page, all of the links are broken, I can't get to any of my JavaScript files. My settings file looks like this: http://pastebin.com/qFVqUXyG
Is there something I am doing wrong? I'm somewhat new to Django.
I guess you're using django-admin runserver to test your website. In that case, have a look at "How to serve static files" (but don't ignore the big fat disclaimer).
Once you're ready to deploy, this chapter contains all the information (provided you go the standard route of Apache/mod_wsgi)
I don't know what the "django way" to include js files is, but I just use a simple regular include in the template, its great since you can dynamically generate the location / filename for these.. oh and if you are using django's test server I don't know how to get it to recognize these or if it even can but all you need is to run an apache server on your local machine and then put the files in the localhost directory and you can include them through localhost by including their full path in the template (i.e., http://localhost/myfiledirectory/myfile.js), also something I do is use amazon s3 to host files since you then get a url for them on there (not that you asked about this but its a quick way to host files if you don't have apache running locally)
Basically, my understanding of Django is that it works differently than a PHP framework, for example, as the files for Django don't sit (or don't need to at least) in the web path (i.e. they do not need to be accessible through the host path but can be anywhere on the machine) this is good for providing extra security, etc but it also means, that to give files a url to download them they need to be in the web hosting path, others may know how to make django do this directly but my quick and dirty method of putting them on the localhost path will work if thats all you're after.
I'm looking for a way to sell someone a card at an event that will have a unique code that they will be able to use later in order to download a file (mp3, pdf, etc.) only one time and mask the true file location so a savvy person downloading the file won't be able to download the file more than once. It would be nice to host the file on Amazon S3 to save on bandwidth where our server is co-located.
My thought for the codes would be to pre-generate the unique codes that will get printed on the cards and store those in a database that could also have a field that stores the number of times the file was downloaded. This way we could set how many attempts we would allow the user for downloading the file.
The part that I need direction on is how do I hide/mask the original file location so people can't steal that url and then download the file as many times as they want. I've done Google searches and I'm either not searching using the right keywords or there aren't very many libraries or snippets out there already for this type of thing.
I'm guessing that I might be able to rig something up using django.views.static.serve that acts as a sort of proxy between the actual file and the user downloading the file. The only drawback to this method I would think is that I would need to use the actual web server and wouldn't be able to store the file on Amazon S3.
Any suggestions or thoughts are greatly appreciated.
Neat idea. However, I would warn against the single-download method, because there is no guarantee that their first download attempt will be successful. Perhaps use a time-expiration method instead?
But it is certainly possible to do this with Django. Here is an outline of the basic approach:
Set up a django url for serving these files
Use a GET parameter which is a unique string to identify which file to get.
Keep a database table which has a FileField for the file to download. This table maps the unique strings to the location of the file on the file system.
To serve the file as a download, set the response headers in the view like this:
(path is the location of the file to serve)
with open(path, 'rb') as f:
response = HttpResponse(f.read())
response['Content-Type'] = 'application/octet-stream';
response['Content-Disposition'] = 'attachment; filename="%s"' % 'insert_filename_here'
return response
Since we are using this Django page to serve the file, the user cannot find out the original file location.
You can just use something simple such as mod_xsendfile. This functionality is also available in other popular webservers such lighttpd or nginx.
It works like this: when enabled your application (e.g. a trivial PHP script) can send a special response header, causing the webserver to serve a static file.
If you want it to work with S3 you will need to handle each and every request this way, meaning the traffic will go through your site, from there to AWS, back to your site and back to the client. Does S3 support symbolic links / aliases? If so you might just redirect a valid user to one of the symbolic URLs and delete that symlink after a couple of hours.