Django Cannot Be Accessed From Localhost - python

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.

Related

Run Flask on 127.0.0.2, get error "Can't assign requested address"

I heard that all the 127.x.x.x are loopback IPs. So I try to run Flask on 127.0.0.2 but It gets error "Can't assign requested address" instead. While it works fine on 127.0.0.1
So how can I run flask on 127.0.0.x ?
Looks like your OS is a MacOS. MacOS using only 127.0.0.1 for loopback interface
But it is possible to assign 127.0.0.XX aliases manually
This article has couple solutions how to do that
Maybe what you looking for is to expose flask api on a different host than localhost(127.0.0.1)
You can choose a different port
app.run(host="0.0.0.0", port=8888)
Or host with machine hostname
from socket import gethostname
gethostname()
For custom hostname, add it in /etc/hosts file, it probably already contains a line about 127.0.0.1, in which case you just add your alias to the list
127.0.0.1 localhost localhost.localdomain myappname
in app
app.config['SERVER_NAME'] = 'myappname:5000'
for public, you would need to configure a specific dns

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

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

Running ubuntu vagrant server won't show anything on debian local browser

I've done this multiple times and never had an issue.
I'm trying to run a website on vagrant (trusty64) through shh on my Debian 8 Jessie x64.
I have used the same Vagrantfile n times before and it always worked perfectly but for I don't know what reason, on my new laptop, when I enter (on vagrant server - tried with sudo and without):
vagrant up
vagrant ssh
cd /opt/site/
python manage.py runserver 0.0.0.0:8000
nothing happens in console. It shows the usual:
System check identified 1 issue (0 silenced).
October 28, 2015 - 10:16:41
Django version 1.8.4, using settings 'settings.development'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
and when I type the ip:port in my local browser it just say:
This webpage is not available
ERR_CONNECTION_REFUSED
Any workaround for this ?
My vagrant config file:
# -*- mode: ruby -*-
# vi: set ft=ruby :
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.provider "virtualbox" do |v|
v.memory = 512
end
config.vm.box = "trusty64"
config.vm.box_url = "https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box"
config.vm.network "private_network", ip: "192.168.100.10"
config.vm.synced_folder ".", "/opt/site"
config.vm.hostname = "alexander"
end
As a mention: I tried multiple ports and still nothing. More, I have the exactly same configuration on another pc and it works flawless.
If changing the port forwarding doesn't work.You might also need to change in the settings of virtual box from NAT to Bridged Adapter.
The firewall will block incoming traffix on port 8000, so you will have to forward that port. Try adding this this to the configuration. you're using:
config.vm.network "forwarded_port", guest: 8000, host: 8080
and then browse to http://192.168.100.10:8080 it should work.
Also dont forget to privision your box again, or even restart just to be sure.
vagrant reload --provision
Why it works on your other machine is a mystery to me. Maybe there is no firewall, or the port is already forwarded?

how to run the django server on hostname?

I wanted to run Django server on a hostname in the local system. Django has its own server ... that runs on the localhost:8000 i just want it to run on
cpanel-django.django-project.com:8000
I tried using:
python manage.py runserver cpanel-django.django-project.com:8000
i am getting this error:
Validating models...
0 errors found
March 06, 2014 - 06:21:24
Django version 1.6.2, using settings 'moclient.settings'
Starting development server at http://cpanel-django.django-project.com:8383/
Quit the server with CONTROL-C.
Error: [Errno -5] No address associated with hostname
You need to edit the hosts file and rename localhost to cpanel-django.django-project.com.
For every host name there is mapping of IP address in your system so that you don't need to worry about IP address, you just need to remember host name.
How to add Host name in your system?
Check this wiki: Host file content
Where to find the file in your system?
Location of file in different system is mentioned here
For windows , you can make an entry in hosts file , located in
C:\Windows\System32\drivers\etc\hosts
and update the file with
127.0.0.1 cpanel-django.django-project.com:8383
If you are running on port 80 ,
127.0.0.1 cpanel-django.django-project.com

Categories

Resources