Running a Python script that creates a webpage on a server - python

I have a Python script that creates a web page displaying some text at localhost port 8080. I have access to a server, and would the script to run there so that it is always running and available anywhere.
I am using the bottle, requests and json libraries.
This is the last line of code in the script that is executed to locally create a web page:
run(host='localhost', port=8080, debug=True)
How would I change this to run on a server? I also have WinSCP and PuTTy to add the script to the public_html directory of the server and I can change permissions. Sorry, I am a novice in this subject.
Thanks for any help!

That depends on the Apache httpd configuration. But, assuming most defaults were left in place and Apache httpd serves index.html, you can just create your webpage (which you tested locally on port 8080) and overwrite that file. But, this only works well if you have a static page and no logic in your Python code. If you want to combine the power of Apache httpd and Python (have the former call the latter) you'll have to use mod_wsgi.
Seeing as you're novice in this subject, I suggest starting off with a simple Apache httpd server according to their quickstart. Then, once you have a few successful requsets under your belt, add mod_wsgi.

Related

Django project is not running on browser

I am following a basic django project, mainly so I can see it run on the browser, because I have another django project where it says it runs on http//127.0.0.1:8000/ but on my browser it shows errors.
I am working linode in cmd on windows laptop, I have edited my firewall on linode to allow ssh, http and https.
https://docs.djangoproject.com/en/4.0/intro/tutorial01/
this is the tutorial I am following.
This is what my code says in cmd.
This is what my browser says.
The tutorial assumes your test server is running on the same machine; 127.0.0.1 is a shorthand for it. Your server is actually running on linode, so you need two things:
expose the server interface outside, as 127.0.0.1 is only accessible internally. The command is: python manage.py runserver 0.0.0.0:8000, see this question for more details.
access it by the remote machine IP/hostname and expose server on external interface. So, in your browser, instead of 127.0.0.1:8000, use <linode_machine_IP>:8000

How to handle Python files with Apache

So I want to get into React Native development and have decided Python to be my backend, but for some reason I cannot configure the Apache correctly. The only way to successfully get the result from the request is to include path to python.exe at the start of the document like so:
!C:\Users\Name\PycharmProjects\AppName\venv\Scripts\python.exe
But the problem is that the file is than executed by the Python console, and if I want to access it via mobile phone I get this error:
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there was an error in a CGI script.
So my question is:
Is there any way in which I can configure Apache to execute a file, without the requirement of the py console, so the request might be handled by devices, which doesn't have a Python console installed?
if you connect from mobile device to http://192.168.1.3/HelloWorld.py on server with CGI then code should be executed on server, not on mobile device. If CGI doesn't work then server may try to send code as normal file and then mobile mdevice ay try to run it locally but it is wrong - CGI server should run code on server.
At start I would put code in subfolder cgi-bin to run it as http://192.168.1.3/cgi-bin/HelloWorld.py because most CGI servers as default run code only in this subfolder.
On Linux script would need shebang
#!/usr/bin/env python
in first line and it should be executable
chmod a+x script.py
CGI has also some rules how to generate data which it will send to client. It may need at start extra information for HTTP protocol - and using only print("Hello World") may generate wrong data and it may have problem to send it. You should have it in any tutorial for CGI scripts. See module cgi
To run Python's code Apache needs module mod_cgi, mod_fcgi or mod_python
mod_cgi and mod_fcgi can run scripts in different languages: Python, Perl, Ruby, etc. and even Bash, PHP or C/C++/Java
Python3 has standard module http which can be used also as simple server
python3 -m http.server --cgi
and it will serve all files in folder in which you run it. And it runs files from subfolder cgi-bin/ - see doc: http

How to make two different python scripts to run on the same port?

Currently I'm developing web services in python using web.py, that serves different functions. Something like
BigQueryService.py
LogicImplementer.py
PostgreService.py
Each service works perfectly when running on the local machine. After deploying on the server, due to I'm refering an other python script, it returns a module error.
Since we have to run all the services on the same port, I pasted all the scripts into a single file named Engine and made it to work using the command
$ nohup python Engine.py 8080 &
Is there any better way to structure the service in web.py? Or is there a way to run all the individual scripts on the same port?
If each service creates its own listener/server socket on the port, then the answer is no. You will need to use the equivalent of an app server that has a single server port and distributes the incoming request to the relevant app (running on the server) based typically on the relative path - so e.g. http://myserver.net:8080/bqs paths get passed to your BiqQueryService, /li to LinkImplementor, /pgs to PostgreService. Flask will do something like this, I'm sure other web service frameworks will too. The server will handle all the communication stuff, pass requests to the app (e.g. bqs) and handle sending response to the client.

Google app engine endpoints api not working in localhost

I am trying to start a python app-engine project on localhost. I am in a Windows 8.1 machine (Python version 2.7). The project code is copied from here link. The server seems to start but the page wouldn't properly load due to some problem. Probably it can't reach the api server.
Here is a shot from firebug showing the failed request
What seems off to me is that in command prompt (I started the dev server from there) the API server is started at some random port other than the module port. But the webpage is trying to access the http://127.0.0.1:8000/_ah/api/static/proxy.html... page at the module port. I tried accessing it at the API server port it does open some page there. (I mean I tried opening http://127.0.0.1:1532/_ah/api/static/proxy.html... seperately) It returns some json I don't understand...
{app_id: dev~udatut-bs, rtok: '0'}
Command prompt log:
Here's the difference between the outputs in localhost and *.appspot.com
Should get this:
But stuck here:(check buttons are not loaded)
I finally found a solution here.
Steps for windows:
Goto your google appengine installation folder and then ./google/appengine/toots. (C:\Program Files (x86)\Google\google_appengine\google\appengine\tools) if you followed default installation instructions.
Open the file appengine_rpc.py and look for the line (towards the end of the file)
opener.add_handler(fancy_urllib.FancyProxyHandler())
and comment it out like
# opener.add_handler(fancy_urllib.FancyProxyHandler())
Close the app and restart it again.

port forwarding django development server - URL is being doubled

I have a Django development server running on a remote centos VM on another lan. I have set up port forwarding using Secure CRT to access the web page through my browser from my desk pc. I am currently not using apache with the development server and is shutdown.
I start the server by running python manage.py runserver 0.0.0.0:80.
When I type either the ip or www.localhost.com into the web browser, my URL is read as if it has been doubled with the host being read as if it was also the path.
Page not found (404)##
Request Method: GET
Request URL: http://www.localhost.com/http://www.localhost.com/
When I try to access the development server from within the same LAN the page loads up fine.
I have been searching through the django documentation and stack overflow, but I have yet to find a similar problem to this. Does anyone have any thoughts on why this may be happening and what could be a possible solution?
Thank you very much in advance!
It looks like the request URL is incorrect:
http://www.localhost.com/http://www.localhost.com/ should probably be http://actual_machine_IP.com/
I'd start searching there. You won't be able to access the VM's port 80 from a different lan using localhost as the hostname since localhost is probably already set in your hosts file.
If you want to test your dev environ remotely, can I suggest either setting up Apache properly with port 80 (as opposed to using django's dev server--privilege restrictions and all that can be circumvented with sudo and other bad practice) or use a pre-built shared dev service like vagrant share.

Categories

Resources