Showing posts with label Webserver. Show all posts
Showing posts with label Webserver. Show all posts

Wednesday, May 13, 2015

Making a simple webapp using flaskr

Most flask examples use a blog as an example. This is probably very similar (and I'm pretty sure 90% was from another tutorial) but I just wanted to show some differences.

First we create a schema, and make a database

schema.sql looks like this.
Before running that you'll need to create the initial db which can be done via the command line in the same folder as your flaskr.py file
sqllite3 flaskr.db < schema.sql

 at the command line type python
>>
from flaskr import init_db
init_db()
This *should* create the first intance of the database.

finally when you're ready, start the following file with
sudo python flaskr.py

So here is the code to run for the simple server(flaskr.py)



So if you want different data stored off, you can change your schema, and the insert/select statement.

If you don't care about storing data, (you just want to perform actions) you can ignore most of the sql related items, and just read the data and perform whatever operation

For example

Now that we have a working app.

If you want to see the app get "data"
go to your browser and type in your ip address (if you did your IP and port as your values) and the data to send for example:
http://192.168.0.1/add?text=yourock&title=thistutorialhopefullyhelps

So now on your python script
request.args.get('title')  should pull out "you rock" and
request.args.get('text') should pull out "this tutorialhopefullyhelps"


Good luck!


This is the site that I had most of my tutorial from:
http://flask.pocoo.org/docs/0.10/tutorial/schema/#tutorial-schema
And the last example some came from here:
http://blog.miguelgrinberg.com/post/designing-a-restful-api-with-python-and-flask

Thursday, January 31, 2013

Using Nodejs as a fileserver


Creating a Simple Fileserver.
Tip #2
I wanted a simple fileserver, I tried looking for a variety of search terms, but finally came upon simple file server. I came across the post linked by Tip #2 above, but it wasn't exactly clear what it was doing exactly.

The documentation isn't 100% clear so I played with it a little, (and added some comments to one of the posters).

I used connect.

Install it with npm connect.

Next use the following code:

var connect = require('connect'),
    http = require('http');
connect()
    .use(connect.static('pathyouwishtoserve'))
    .use(connect.directory('pathyouwishtoserve'))
    .listen(8080);
.use(connect.static('pathyouwishtoserve')) tells the server where to look for files.
.use(connect.directory('pathyouwishtoserve')) tells the server to return the following directories contents.

If you now navigate to http://your-ip-address:3000 you'll get a list of the directory contents,
if you add a /filename to that path you'll get the file.


I'll be posting tips as I come across them of using NodeJS as an intranet fileserver.
Long story short, I want to build a Roku app that will refer to the NodeJS server for where to find the files. I figure it'll be simpler than PLEX, and also faster (I'm hoping). Once I get the stuff running I'll plan on running it on my Pogo Plug as a media server for my Roku, totally awesome!

Sunday, January 27, 2013

Using NodeJS as an Intranet Webserver

I'll be posting tips as I come across them of using NodeJS as an intranet webserver.

Long story short, I want to build a Roku app that will refer to the NodeJS server for where to find the files. I figure it'll be simpler than PLEX, and also faster (I'm hoping). Once I get the stuff running I'll plan on running it on my Pogo Plug as a media server for my Roku, totally awesome!

Tip #1
When running a http://nodejs.org/ port, the IP address should be of the machine you are currently on. I chose the port 8080 as it's the standard http port.

From linux you can find this by doing a simple ifconfig command on the command line. From windows ipconfig should do it.

Then using the example below: (from the NodeJS website)

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(8080, '100.11.1.6');
console.log('Server running at http://100.11.1.6:8080/');

So when you go to another machine such as your phone and access the page 100.11.1.6 you should get "Hello World". When you're able to do this, your Roku can now see the server (when you have the app setup).

The above tip was discovered after a google search to figure out an error. The link is from stackoverflow, and can be clicked above on the Tip #1 link.