Customizing Django's server configurations - python

On firing up python's development server: >$ python manage.py runserver, by default it provides a default IP address and such, with the output:
>$ python manage.py runserver
Validating models...
0 errors found
February 12, 2014 - 23:20:35
Django version 1.6.2, using settings 'counter.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
How do I provide my own settings for an HTTP server? I want to specify my own port, ip address, as well as my own request handler. I added the following code in my models.py file, but it doesn't seem to be running. No print statements are being shown. My code is below:
def run():
print "starting run"
port = 8080
server_address = ('127.0.0.1', port)
httpd = HTTPServer (server_address, Counter_HTTPRequestHandler)
httpd.server_forever()
assert False # unreachab;e
if __name__ == '__main__':
run()
Am I supposed to provide this code somewhere else? I understand I can specify my own IP and PORT as arguments to runserver, but how I do execute my main - run() method so that I can specify my custom http request handler?

runverver takes port as an argument, run it as manage.py runserver 8080 to run on 8080
For more complex requirements, you might want to try something like uwsgi.
None of your print statements added in models.py work because when you runserver, you don't run models.py, you just import models from it.

Related

How can I give specific IP and port that my code requires

In django I am using python osc, using osc server which required IP address and Port no. to run.
When I run that specific file on terminal I add the ip and port like:
OSC.py --ip --port
Now I want to implement this in django but don't know where to add ip and port
If you are talking about python manage.py runserver command then you can pass the ip and port number as well.
Ex-1: python manage.py runserver 192.168.0.100:5001
Ex-2: python manage.py runserver 5001 # only port overriding
Ex-3: python manage.py runserver 192.168.0.100 # ip only

Whats of this line `eventlet.wsgi.server(eventlet.listen(('', 8000)), application)`'s function?

I set the bellow code in the wsgi.py,:
eventlet.wsgi.server(eventlet.listen(('', 8000)), application)
if I use the:
python3 manage.py runserver 0.0.0.0:8001
it will ignore the 8001 port of python3 manage.py runserver 0.0.0.0:8001?
because the logs are:
$ python manage.py runserver 0.0.0.0:8001
Performing system checks...
System check identified no issues (0 silenced).
May 09, 2018 - 10:48:35
Django version 1.11.5, using settings 'Qiyun02.settings'
Starting development server at http://0.0.0.0:8001/
Quit the server with CONTROL-C.
(4516) wsgi starting up on http://0.0.0.0:8000 # you see this is using 8000
If this line settings:
eventlet.wsgi.server(eventlet.listen(('', 8000)), application)
('', 8000) means : 0.0.0.0:8000?
There is a simple profile of evenlet wsgi, in your wsgi.py you use the eventlet wsgi as the application's wsgi.
eventlet.wsgi.server(eventlet.listen(('', 8000)), application)
the eventlet.listen() is a param of eventlet.wsgi.server(), and the eventlet.listen() means listen which address and port.
the ('', 8000) combine the address and port. if we do not set the first param, it will be default 0.0.0.0.
if we set the localhost it will be look back address 127.0.0.1.
and we also can set a IP address of our computer's.
We can use ifconfig -a to list the available IP addresses of our *nix computer.
use ipconfig -a to list the Windows's.

Which server Django is using?

After we installed the Django, we use the command:
python manage.py runserver
we can run a server, but I don't know the server is which server, and I searched the django directory, find the server is not in the directory:
aircraftdeMacBook-Pro:Django-1.11.2-py2.7.egg ldl$ cd django/
aircraftdeMacBook-Pro:django ldl$ ls
__init__.py bin dispatch shortcuts.pyc utils
__init__.pyc conf forms template views
__main__.py contrib http templatetags
__main__.pyc core middleware test
apps db shortcuts.py urls
So, I have two questions:
The server is which web-server, and how can I find its location?
If we finish the application, if we use the web-server as the python-web's web-server(means why not apache or nginx)?
The port default is 8000, how to change to the 80?
1) Django provides a default wsgi server as #sach20 mentioned.
2) The django server should be used for development. I personally use nginx and gunicorn to run my server. You can find a tutorial on how to set on up here:
Tutorial
3) You can run:
python manage.py runserver 0.0.0.0:80
You can substitute 80 with any port you want to use.
Django runserver is a local dev server, should not be used in production... APACHE AND NGINX are different things.
Django provides a simple WSGI server as a dev server.
Also id guess you have not looked inside the django docs because what you are looking for is right here
Examples of using different ports and addresses¶
Port 8000 on IP address 127.0.0.1:
django-admin runserver Port 8000 on IP address 1.2.3.4:
django-admin runserver 1.2.3.4:8000 Port 7000 on IP address 127.0.0.1:
django-admin runserver 7000
Django provides a default wsgi development server

Django Cannot Be Accessed From Localhost

It's kinda weird I cannot access django from localhost but I able to access it from local IP.
python manage.py runserver 0.0.0.0:8000
then when I try to access from
My host file
127.0.0.1 lmlicenses.wip4.adobe.com
127.0.0.1 lm.licenses.adobe.com
127.0.0.1 gc.kis.scr.kaspersky-labs.com ff.kis.scr.kaspersky-labs.com ie.kis.scr.kaspersky-labs.com
0.0.0.0 gc.kis.scr.kaspersky-labs.com ff.kis.scr.kaspersky-labs.com ie.kis.scr.kaspersky-labs.com
I'm guessing something wrong with windows firewall or kaspersky,but I don't know what to do.
I've add exception to port 8000 and python.exe too
I cannot access localhost:8000 from my browser if I run python manage.py runserver
I don't know why that command cannot work in my laptop, but in my pc it just works.
====
ok, so my solution now is to use ipv6 and port 8000 which is
python manage.py runserver [::]:8000
Just try http://0.0.0.0:8000 instead of localhost:8000
Default localhost value is http://127.0.0.1
If you see the runerver result you have :
starting developement server at http://0.0.0.0:8000
Because you told that django server start at http://0.0.0.0:8000 when you run this command :
python manage.py runserver 0.0.0.0:8000
I'm also beginner to django, but I needed following things in order to run the server:
In settings.py, added ALLOWED_HOSTS as
ALLOWED_HOSTS = [
'elearning.com'
]
where elearning.com is my hostname. You can have multiple hosts separated with comma.
Used following command to run the server:
python manage.py runserver [::]:8000 or python manage.py runserver 0.0.0.0:8000 or python manage.py runserver 0:8000.
It will now listen to all interfaces. Read Django doc here.
Browsed to given hostname. In my case, it is http://elearning.com:8000/
I tried writing IP in ALLOWED_HOSTS but it didn't work, I could not open http://192.168.x.x:8000 in browser. Can anyone please correct me?
simply execute
python manage.py runserver
automatically you can access by localhost and for the extern access you could have access if not are it's locked the port 8000, for access to django admin use the next url:
http://127.0.0.1:8000/admin/
How your host file end up like this ?? . Can you test something.
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
My host file see everything is commented out. Can you do the same with your hosts file and give it a Try .. .
In the settings.py file in your project folder,
add '0.0.0.0' to the allowed host list.
Then save the file and run the command.Guess will work.

flask manage.py runserver can't pass parameter

user manager.py runserver my flask webframework can start on http://127.0.0.1:5000 but it can not access on other computer in network.
so i need use an open IP in network.
although i use bellow command:
manage.py runserver 192.168.49.25:8000
it can not run and give a error info:
manage.py: error: unrecognized arguments: 192.168.49.25:8000
I don't known what's wrong with it??
If you want to use Flask-Script (python manage.py runserver) to run your Flask Application you can use the parameter --host to run it on a public IP.
python manage.py runserver --host 0.0.0.0
see also: https://flask-runner.readthedocs.org/en/latest/
Read the documentation:
Externally Visible Server If you run the server you will notice that
the server is only accessible from your own computer, not from any
other in the network. This is the default because in debugging mode a
user of the application can execute arbitrary Python code on your
computer.
If you have debug disabled or trust the users on your network, you can
make the server publicly available simply by changing the call of the
run() method to look like this:
app.run(host='0.0.0.0') This tells your operating system to listen on
all public IPs.
If you're already using Flask-Script then you have a way to get your host defined in your code.
from yourapp import create_app
from flask_script import Manager, Server
from yourapp import config
app = create_app(config.DevelopmentConfig)
manager = Manager(app)
manager.add_command("runserver", Server(host=app.config['HOST'], port=app.config['PORT']))
if __name__ == '__main__':
manager.run()
now the server host and port will come from config

Categories

Resources