How to Host Python, Django, PostgreSQL Application on IIS 10 - python

I am trying to host my Django application on Windows 2016 server IIS server. I had used Python, Django, pipenv as virtual environment and PostgreSQL as Database.
How to host is using a Domain Name ?
I had tried almost tried everything available on the internet but till now I am not successful.
Maybe I have not got the perfect tutorials or find the correct one to host the Django Application.
Please help me host the Django Application on IIS 10.
I will be really grateful for the help
Thanks in advance.
Error occurred while reading WSGI handler:
Traceback (most recent call last):
File "c:\users\sachin kumar\.virtualenvs\login-zru7l_54\lib\site-packages\wfastcgi.py", line 791, in main
env, handler = read_wsgi_handler(response.physical_path)
File "c:\users\sachin kumar\.virtualenvs\login-zru7l_54\lib\site-packages\wfastcgi.py", line 633, in read_wsgi_handler
handler = get_wsgi_handler(os.getenv("WSGI_HANDLER"))
File "c:\users\sachin kumar\.virtualenvs\login-zru7l_54\lib\site-packages\wfastcgi.py", line 586, in get_wsgi_handler
raise Exception('WSGI_HANDLER env var must be set')
Exception: WSGI_HANDLER env var must be set
StdOut:
StdErr:

Check your settings of FASTCGI Module and add needed environment variables
Full docs from Microsoft to run python scripts on IIS are here
offtop: don't use space in your user_name (and in the user folder respectively).

Related

Can't host my django website on shared hosting

I am trying to deploy my django project on shared hosting. I am trying from couple of hours but still now I can't host my django website and getting "Internal Server Error Error 500".
this is my root passenger_wsgi.py
from my_root_folder_name.wsgi import application
#setting.py
ALLOWED_HOSTS = ['mydomain.com','www.mydomain.com']
I also installed python app and django on my server. Now I am hopeless. I am trying from last five hours for host an website but still now I can't. Where I am doing mistake???
I am seeing those error from error log:
File "/home/sellvqed/virtualenvass/dfdf/3.8/lib/python3.8/site-packages/django/apps/registry.py", line 83, in populate
raise RuntimeError("populate() isn't reentrant")
RuntimeError: populate() isn't reentrant
Finally I solved my problems after delete all apps and virtual folder from my hosting account. T

I can't migrate on heroku. Using Django MySQL

I can't migrate on Heroku. Using Django and MySQL.
I don't what wrong with it.
There are some wrongs on setting_mysql.py?
I got an error like this.
(base) mypc#mypc website % heroku run python manage.py migrate
Running python manage.py migrate on ⬢ myapp... up, run.3308 (Free)
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 217, in ensure_connection
self.connect()
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 195, in connect
self.connection = self.get_new_connection(conn_params)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/mysql/base.py", line 227, in get_new_connection
return Database.connect(**conn_params)
File "/app/.heroku/python/lib/python3.9/site-packages/MySQLdb/__init__.py", line 130, in Connect
return Connection(*args, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/MySQLdb/connections.py", line 185, in __init__
super().__init__(*args, **kwargs2)
MySQLdb._exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
You have provided the wrong database access credentials, because if I'm not mistaken - you can't connect to the database via socket.
Try official solution:
On Heroku, sensitive credentials are stored in the environment as
config vars. This includes database connection information (named
DATABASE_URL), which is traditionally hardcoded in Django
applications.
The django-heroku package automatically configures your Django
application to work on Heroku. It is compatible with Django 2.0
applications.
It provides many niceties, including the reading of DATABASE_URL,
logging configuration, a Heroku CI–compatible TestRunner, and
automatically configures ‘staticfiles’ to “just work”.
Installing django-heroku:
pip install django-heroku
Be sure to add django-heroku to your requirements.txt file as well.
Add the following import statement to the top of settings.py:
import django_heroku
Then add the following to the bottom of settings.py:
Activate Django-Heroku:
django_heroku.settings(locals())
Deploy, and you should be good to go!
I'm using this solution and it works for me.
Another option is to copy the database access data in the database panel and manually put it into settings.py

PermissionError: How to read a file from a remote file server (accessible by IP) with authentication?

How can I read a file from a remote file server (accessible by IP) using Python3? How to set the user and password with this IP file server setup?
Example of file location (with authentication needed): \\10.123.123.132\example.jpg
The function I tried is as below which is throwing a PermissionError error. I am using django as a web framework. When the file server access is pre-authenticated via Windows authentication (manually) and django project is run locally on localhost, the code works fine. However, when the django project is served using with winsw and waitress (https://github.com/winsw/winsw, https://docs.pylonsproject.org/projects/waitress/en/stable/runner.html), the file server access code fails to work with PermissionError logged.
def copy_file(fullname, filename):
with open(filename, 'wb') as output:
data_file = open(fullname, "rb")
output.write(data_file.read())
output.close()
Error:
Traceback (most recent call last):
File "D:\app\services.py", line 21, in copy_file
data_file = open(fullname, "rb")
PermissionError: [Errno 13] Permission denied: '\\\\10.123.123.132\\example.jpg'
Will appreciate any feedback and clue, thank you very much.
By default, services run as a special system user that does not have permission for your file shares. Have you set the username for your service?
You can set the user name in the services control panel. Run services.msc to bring up the control panel, find your service, and edit its parameters.
By the way, you can use import shutil / shutil.copyfile(src,dst) to copy files by name.

ConnectionFailure when connecting to MongoHQ URI via Flask-PyMongo on httpd

I have a Flask app with a Mongo backend working perfectly in my local dev environment. I moved it to an AWS-hosted RHEL instance and got the Flask app working via httpd/mod_wsgi. However, connecting to a DB on MongoHQ continues to fail.
The code for the connection is this:
from flask import Flask
from flask.ext.pymongo import PyMongo
app = Flask(__name__)
app.config["MONGO_URI"] = 'mongodb://myusername:mypasswd#myhost.mongohq.com:myport/mydb'
mongo = PyMongo(app)
A sample query:
#app.route('/books')
def books()
all_books = mongo.db.listings.distinct("bookinfo")
return all_books
The error message from Apache (edited for readability):
mod_wsgi (pid=5116, process='myProcess', application=''): Loading WSGI script '/route/to/myapp.wsgi'.
mod_wsgi (pid=5116): Target WSGI script '/route/to/myapp.wsgi' cannot be loaded as Python module.
mod_wsgi (pid=5116): Exception occurred processing WSGI script '/route/to/myapp.wsgi'.
Traceback (most recent call last):
File "/route/to/myapp.wsgi", line 10, in <module>
from myApp import app as application
File "/route/to/myapp.py", line 8, in <module>
mongo = PyMongo(app)
File "/route/to/my/venv/lib/python2.6/site-packages/flask_pymongo/__init__.py", line 98, in __init__
self.init_app(app, config_prefix)
File "/route/to/my/venv/python2.6/site-packages/flask_pymongo/__init__.py", line 230, in init_app
cx = connection_cls(*args, **kwargs)
File "/route/to/my/venv/python2.6/site-packages/pymongo/mongo_client.py", line 352, in __init__
raise ConnectionFailure(str(e))
ConnectionFailure: could not connect to myhost.mongohq.com:myport: [Errno 13] Permission denied
My thoughts/investigation:
The app runs fine when I remove the DB Connection: So it's not a
mod_wsgi, Apache, or permissions issue?
I can connect to the mongo uri via the Mongo shell and Python shell but not through the app: So it's not a firewall issue?
MongoHQ's ultraresponsive customer service (seriously, they're awesome) believes it's an issue with the Flask-PyMongo driver, but I have no idea how to debug that.
Any help/hints/possibilities would be appreciated.
SELinux was the root cause for this. I'd disabled the firewall but never turned off SELinux by editing the /etc/sysconfig/selinux file. After doing so, the app still had issues, but this logging code helped tremendously:
import logging
file_handler = logging.FileHandler(filename='/var/www/myapp/flaskerror.log')
file_handler.setLevel(logging.WARNING)
app.logger.addHandler(file_handler)

Google App Engine Launcher Does Not Launch Even If PIL Is Installed [duplicate]

Google-app-engine development server runs great yesterday, but when I try to start it today. It only shout out this Error.
I tried use lsof -i:8080 / lsof -i:8000 to make sure these ports are not taken.
I also tried use a --port arg to switch to another port.
I even removed the gae folder and installed a new one.
-- with no luck at all.
Maybe there is a obvious solution but I can't see it.
Here is the Oh-My-God trace stack..
Traceback (most recent call last):
File "/home/henry/software/google_appengine/dev_appserver.py", line 182, in <module>
_run_file(__file__, globals())
File "/home/henry/software/google_appengine/dev_appserver.py", line 178, in _run_file
execfile(script_path, globals_)
File "/home/henry/software/google_appengine/google/appengine/tools/devappserver2/devappserver2.py", line 689, in <module>
main()
File "/home/henry/software/google_appengine/google/appengine/tools/devappserver2/devappserver2.py", line 682, in main
dev_server.start(options)
File "/home/henry/software/google_appengine/google/appengine/tools/devappserver2/devappserver2.py", line 653, in start
apis.start()
File "/home/henry/software/google_appengine/google/appengine/tools/devappserver2/api_server.py", line 152, in start
super(APIServer, self).start()
File "/home/henry/software/google_appengine/google/appengine/tools/devappserver2/wsgi_server.py", line 294, in start
raise BindError('Unable to find a consistent port %s' % host)
google.appengine.tools.devappserver2.wsgi_server.BindError: Unable to find a consistent port localhost
Exception in thread Thread-4 (most likely raised during interpreter shutdown):
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
File "/usr/lib/python2.7/threading.py", line 763, in runhenry#henry-A
This can be caused by multiple entries in your hosts file for 'localhost'.
For example in file /etc/hosts:
127.0.0.1 localhost
127.0.0.1 mymachinename localhost
if you delete all mappings but one for localhost, the problem will hopefully be resolved.
127.0.0.1 mymachinename localhost
This is a known issue and as far as I understand it will be corrected in a future release.
While I never seen that before try running it on a different port or even using a different host:
dev_appserver.py /path/to/project --port 8888 --host 127.0.0.1
Where for host add your current IP address.
Similar to what was posted, I had this issue and fixed it by altering the hosts file. The issue was with IPv6 addresses redirecting to localhost:
In my hosts file I had
127.0.0.1 localhost
::1 localhost
fe80::1%lo0 localhost
And I commented out the IPv6 addresses to give
127.0.0.1 localhost
#::1 localhost
#fe80::1%lo0 localhost
I'm not sure this is a viable permanent solution as I imagine it's important to have the IPv6 numerical addresses for localhost to be in the hosts file but it works for now until a proper fix is released.
I suppose there is a bug in the google app engine.
I debuged appengine/tools/devappserver2/wsgi_server.py, and here is the facts:
1. it runs fine when internet is disconnected
2. it shows such error when internet is on.
280 addrinfo = socket.getaddrinfo(host, port, socket.AF_UNSPEC,
281 socket.SOCK_STREAM, 0, socket.AI_PASSIVE)
In this piece of code, if you connect internet, addrinfo will only have the address in public internet. even you assign port and host in command line. Then you have no chance to bind this socket to localhost, since the address you bind is the public address you are using now.
In order to solve it, I just change the code into
280 addrinfo = socket.getaddrinfo(host, port, socket.AF_UNSPEC,
281 socket.SOCK_STREAM, 1, socket.AI_PASSIVE)
It works well now, I didn't check the code about socket.getaddrinfo, however, I suppose that it functions as ignoring the lookup address or not according to the integer 0 or 1.
Btw, I am using MacOs, there could be system dependency problem as well, if this is the case, then socket package should redesign somehow.
dev_appserver.py . --port 4000
this fixed it for me.
I solved this issue by passing an explicit api port to dev_appserver.py:
dev_appserver.py path/to/project --api_port 3000
I was unable to solve the issue by editing the hosts file or passing --port / --host to dev_appserver.py.
I didn't identify the root cause.
In my case just restarting the terminal worked.

Categories

Resources