HighCharts doesnt get data from Flask endpoint - flask

I use the code of "Live data from dynamic CSV" from Highcharts Demos as a basis for my need. As it gets data lively from a web service url, I just change it to get data from my Flask endpoint. That is to say, I use the same code to be served with Flask.
Even if the flask endpoint serves the same content, I don't get it to work. I just changed the url of "https://demo-live-data.highcharts.com/time-data.csv" with /data. And here /data serves the totally same kind of csv output.
So far I could not obtain the result.
Demo code located in "https://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/highcharts/demo/live-data/" is used.
var defaultData = '/data';

I found out that I have to use "http://10.0.2.2:5000/data" instead of "0.0.0.0:5000/data". Because 10.0.2.2 is the special alias to host loopback interface (aka 127.0.0.1).

Related

How does Web Browser open offline ip address just like Jupyter notebook?

I was using Jupyter notebook and was wandering how does it works offline. Where does server is? How TCP connection is made? How does htpp request is sent?
Similarly when we are working on some website project (eg: making one website in django) when you compile that html code in your terminal, it provides you an output with an ip address and when you run that ip address in your browser, browser will show you your website. So how does this work and how that ip address it generated? Can anybody please explain me?
The browser sends a Http request to the server.
The server does its magic and dumps the request via the CGI to django.
Some part of django receives the request and turns it into a django request object.
The request object wanders on some nebulous paths through the middleware which does strange things with it.
The request object finally ends up in some function which looks at the urls, takes the patterns out of urls.py and calls up a view function.
The view functions do their magic (with models and templates as partners) in, this is probably where I have the strongest illusion of understanding (well, apart from the database abstraction magic, that is... ;)
The view functions returns an HttpResponse object, I guess this is returned on some nebulous paths to the CGI.
Webserver takes over again and sends the Http response to the client.

How to document APIs in postman in the following conditions?

We have a series of GET requests api, say in the format of
curl -XGET ip-address/api-keywords
These kind of requests generate a JSON responses.
Now the issue here is when I document using Postman,we do not want the ip address to be exposed. How to edit that in postman?.
You can setup environments in Postman that will let you use variables. Then in your collection, different developers or users can setup their own environments and variables. See https://www.getpostman.com/docs/environments

HTSQL shell's Get Request Not compatible with Gunicorn+Nginx for long queries

I am using HTSQL with Django. I use HTSQL shell to check/generate my queries and then use them for rendering data in json and raw formats.
so like, my HTSQL shell url is:
http://127.0.0.1:8000/htsql
so when I want to access data from a table in the HTSQL shell environment, I do,
http://127.0.0.1:8000/htsql/table_name
and to get JSON data,
http://127.0.0.1:8000/htsql/table_name/:json
In background, HTSQL shell fetches this data by using a GET request. So from my client-side Javascipt/jQuery, I initiate a GET request with its URL in above format and get my desired JSON data directly.
Everything was fine when I was using local Django server, but when I deployed my project using Gunicorn and Nginx, it naturally started to block some of my long(actually, pretty long) queries in the GET requests. I searched this problem and found out that Gunicorn allows GET request values ranging from 0 to 8190 characters. So I tweaked my Gunicorn settings for the maximum limit but still the same problem. This was because my queries, when used with several filter values, are exceeding 8190 limit.
So I thought to use POST request as its normally preferred for secure and long requests. So I changed my GET request to POST request and pointed it to the same URL as mentioned above and tried it on my local Django Server(i.e without Gunicorn and Nginx). But now I get "400 BAD REQUEST". With firebug, I checked that the response was "POST requests are not permitted."
I also noticed that the HTSQL_Django Module routes all the request to htsql_django.views.gateway. I had a look to this gateway function in the views.py of htsql_django module but couldn't find any clue.
Is it so, that the HTSQL doesn't accepts POST requests?? How can I fetch/access JSON data from HTSQL using POST request?

Microsoft Azure appending extra query string to urls with query strings

In deploying a version of the Django website I'm working on to Microsoft's Azure service, I added a page which takes a query string like
http://<my_site_name>.azurewebsites.net/security/user/?username=<some_username>&password=<some_password>
However, I was getting 404 responses to this URL. So I turned on Django's Debug flag and the page I get returned said:
Page not found (404)
Request Method: GET
Request URL: http://<my_site_name>.azurewebsites.net/security/user/?username=<some_username>&password=<some_password>?username=<some_username>&password=<some_password>
Using the `URLconf` defined in `<my_project_name>.urls`, Django tried these URL patterns, in this order:
^$
^security/ ^user/$
^account/
^admin/
^api/
The current URL, `security/user/?username=<some_username>&password=<some_password>`, didn't match any of these.
So it seems to be appending the query string onto the end of the url that already has the same query string. I have the site running on my local machine and on an iis server on my internal network which I'm using for staging before pushing to Azure. Neither of these site deployments do this, so this seems to be something specific to Azure.
Is there something I need to set in the Azure website management interface to prevent it from modifying URLs with query strings? Is there something I'm doing wrong with regards to using query strings with Azure?
In speaking to the providers of wfastcgi.py they told me it may be an issue with wfastcgi.py that is causing this problem. While they look into it they gave me a work around that fixes the issue.
Download the latest copy of wfastcgi.py from http://pytools.codeplex.com/releases
In that file find this part of the code:
if 'HTTP_X_ORIGINAL_URL' in record.params:
# We've been re-written for shared FastCGI hosting, send the original URL as the PATH_INFO.
record.params['PATH_INFO'] = record.params['HTTP_X_ORIGINAL_URL']
And add right below it (still part of the if block):
# PATH_INFO is not supposed to include the query parameters, so remove them
record.params['PATH_INFO'] = record.params['PATH_INFO'].split('?')[0]
Then, upload/deploy this modified file to the Azure site (either use the ftp to put it somewhere or add it to your site deployment. I'm deploying it so that if I need to modify it further its versioned and backed up.
In the Azure management page for the site, go to the site's configure page and change the handler mapping to point to the modified wfastcgi.py file and save the configuration.
i.e. my handler used to be the default D:\python27\scripts\wfastcgi.py. Since I deployed my modified file, the handler path is now: D:\home\site\wwwroot\wfastcgi.py
I also restarted the site, but you may not have to.
This modified script should now strip the query string from PATH_INFO, and urls with query strings should work. I'll be using this until I hear from the wfastcgi.py devs that the default wfastcgi.py file in the Python27 install has been fixed/replaced.

Serving protected static media from django/nginx + streaming from a 3rd party app

We want to serve protected media from django, using something similar to the django nginx x-accel-redirect setup.
The only problem is that the static files are not located on the public facing django/nginx machine, but in a internal machine that streams the file via http/rest api.
Currently we download the file on the nginx machine and serve it via nginx x-accel-redirect, but we want to optimize this part and looking for options. x-accel-redirect has known problems with files that are streamed from another source.
We are contemplating using django itself as a quasi buffer, but are open to other options as integrating something like whizzer/twisted, or maybe even having another service altogether.
What would be the best option for serving those static files and preserving security?
Use: http://www.allbuttonspressed.com/projects/django-filetransfers
Make your own Django storage backend for the internal machine's http/rest api, that returns
a File object, and pass that object to filetransfer's server_file function.
That's how I do it in Mayan EDMS https://github.com/rosarior/mayan/blob/master/apps/documents/views.py#L300
django-storages' backends could help you get started.
https://bitbucket.org/david/django-storages/wiki/Home
Update:
Django_resto appears to have an http based storage class
https://github.com/aaugustin/django-resto/blob/master/django_resto/storage.py#L62
I had success doing something similar using django-http-proxy. This assumes that the image server is at least as reliable as the django server.
Then in my urls, I simply mapped the url to the http proxy view, something like:
(r'^protected/.*$', 'httpproxy.views.proxy'),
Then configured PROXY_FORMAT accordingly.
Implement a simple one-shot signature system in the media machine, using any very thin (django is OK, as it does not need to get to the database) code layer, and x-accel-redirect in nginx.
In the auth machines, generate the correct signature only when the user is allowed to get the resource, and return a 302 to the signed media.
The signature could be time-based, expiring in a fraction of a second, so a sniffer can't use the URL again.
You could use lighttpd to handle the streaming. It has a nice module to protect resources with signatures: http://redmine.lighttpd.net/wiki/1/Docs:ModSecDownload
So I'm thinking you could have nginx just proxy to the streaming server (that's lighttpd).
It's pretty easy to cook up the signature, here's a python example: init.py#cl-27">https://bitbucket.org/ionelmc/django-secdownload-storage/src/be9b18701015/secdownload_storage/init.py#cl-27