How can I provide the Rest Framework API to intranet machine? - python

I use the rest framework write APIs, and when I runserver the website
I can access the APIs in the development machine(the intranet ip is 10.10.10.111):
But how can I access the API in other computer which is in the 10.10.10.0/24 intranet?
If can, this will become more convenient for our team to debugging.

To make your development server view-able to other machines on the network, use its own IP address (e.g. 10.10.10.111) or 0.0.0.0.
See the examples for running django projects on different ports and ip address.
The command will be:
django-admin runserver 0.0.0.0:<port>
or
python manage.py runserver 0.0.0.0:<port>
You can then visit the site in 10.10.10.111:<port> or <your.machine.ip.address:port> from same network.

What #salman said plus: This is most likely the host computers firewall blocking the packets. Depending on your OS try to selectively open this port.
Edit
Most likely the firewall on the host (*) machine will block the traffic. Also ask your admin if a firewall in the network blocks the port.
*) host meaning „the machine running the server

Runserver in Django project:
python manage.py runserver 0.0.0.0:<port>
stop the firewall in your develop OS, such as the CentOS7:
systemctl stop firewalld.service
then add the ALLOW_HOST in your project settings.py,
because your computer are connected use inner network, you can allow all:
ALLOWED_HOSTS = ['*']

Related

How to deploy a wep abb within my work network?

I created an API for the company i work however i would like people having access in the same company network, how can i achieve this? I currently modified the allowed host as follow:
ALLOWED_HOSTS = ['127.0.0.1', 'localhost','192.168.6.7', '127.0.1.1', '161.19.109.123']
however only work in my computer under IP: 127.0.0.1:8000, any suggestions? FYI i do not have administrator privilege.
Try this to access project in a lan server
python manage.py runserver your_ip:port.
For example
my system IP address is something like 192.168.12.135, when we run run django development server its runs at localhost:8000, i mean with the system IP address and with a port 8000 like 192.168.12.135:8000 right.
it'll be accessible if you're running through apache or any other webservers other than the development server.
I would advice you to host and use a local apache server rather than using development server. Doing so, you can foresee the issues you'll be facing when moving to production.

Django is not available from forwarded port

I'm trying to connect to my website from another node on another network. If the nodes are in the same network, i can connect to the website without a problem.
I've forwarded port for ssh and Django (8000), I also have apache ready on port 9080.
ssh and apache ports work fine when connecting to them from external ip address, Django does not for some reason.
First, i tried to run the server on port 8000:
python manage.py runserver 0.0.0.0:8000
This works completely fine when connecting from the node that is in the same network as the server, but for some reason, whenever i try to access it from external ip address, the connection is refused.
To make sure it was Django, I also tried running the server on the same port as Apache (9080), although, i didn't expect "errorless" response, since i knew that port was occupied. But there was no change at all, I was still getting the same Apache page that i would get before.
I also tried allowing port 8000 on firewall:
sudo ufw allow 8000/tcp
But pretty sure this is not the problem, since this Debian came without any firewall.
I also tried to empty ALLOWED_HOSTS in settings, but there was no progression.
It seems like Django has no effect for external connections, what could be the reason?
I also struggle to understand the purpose of other http web server platforms in this case (e.g Apache, Nginx), Isn't Django creating a webserver itself along with its custom wsgi?
Firewall is not the problem, neither is the web server, then may the problem be caused by the Django itself? Maybe it is outer firewall?
It is not clear how you are configuring Apache to forward requests to Django, it seems like you are treating those as two independent components. If you want to use a web server in front of Django (recommended for production envs), you need to configure both Apache and Django.
Then, as you are running django in dev mode (python manage.py runserver 0.0.0.0:8000), you should reach Django in http://server_ip:8000 regardless of Apache, perhaps there is another firewall blocking the connection. Use tracert / traceroute to find out where the connection is blocked.
FInally, for production environments, it is recommended to use a web server in front of Django to increase security and performance. See the docs for further information.
My guess is that you have another firewall blocking the port. You opened the local firewall using ufw, but there may be an outer firewall.
python manage.py runserver 0.0.0.0:8000 starts correctly? If so, keep an eye in the log.
Inside the server, do a request wget http://localhost:8000. The request should be logged
If you can reach Apache in port 9080 from outside the server, you can:
Use nmap to find the opened / closed / filtered ports in the server to find if there is another firewall inbetween.
Configure Apache to forward requests to Django, although this does not solve the problem
In your question you say that you have forwarded port for ssh and Django. What exactly is this? Are you sure that you have not misconfigured your ssh server to listen in port 8000?

Access Django app from other computers

I am developing a web application on my local computer in Django.
Now I want my webapp to be accessible to other computers on my network. We have a common network drive "F:/". Should I place my files on this drive or can I just write something like "python manage.py runserver test_my_app:8000" in the command prompt to let other computers in the network access the web server by writing "test_my_app:8000" in the browser address field? Do I have to open any ports and how can I do this?
It is should be done with central system or server.
By default manage.py runserver will not give ip bind permission. So
Note that the default IP address, 127.0.0.1, is not accessible from other machines on your network. To make your development server viewable to other machines on the network, use its own IP address (e.g. 192.168.2.1) or 0.0.0.0 or :: (with IPv6 enabled).
If you want to check in your local machine then follow
python manage.py runserver 0.0.0.0:8000
Now go to your network computer and access your ip like 192.168.1.24:8000
Updated:
For Django version about 1.10 you should add your host to ALLOWED_HOSTS here
Just add your own IP Address to ALLOWED_HOSTS
ALLOWED_HOSTS = ['192.168.1.50', '127.0.0.1', 'localhost']
and run your server python manage.py runserver 192.168.1.50:8000
and access your own server to other computer in your network
Run the application with IP address then access it in other machines.
python manage.py runserver 192.168.56.22:1234
Both machines should be in same network, then only this will work.
This can be done in just 4 steps:
Make sure in settings.py: ALLOWED_HOSTS = ['*']
Run the server using: python manage.py runserver 0.0.0.0:3000
Go to Windows security -> Firewall & network protection and turn off the windows firewall completely.
Now at this point, your Django API can be accessed within your local network using the URL: http:// Your-LAN-IP-address:3000/
Now we have to open port 3000. Go to http://192.168.1.1 and login into your router. Now go to Advanced tab -> NAT -> Virtual server and fill the following fields alone:
WAN port - 3000
LAN port - 3000
LAN IP address - check your machine
Now at this point, your Django API can be accessed from anywhere using the URL:
http:// Your-Public-IP-address:3000/
This is very very simple
python manage.py runserver 0.0.0.0:8000
change in setting.py
ALLOWED_HOSTS = ['*']
open linux terminal
sudo ifconfig
Get inet ipv4 address
Run on other same local network PC
Check Your Computer's Ip Address
These commands will tell you all network info
ip add
or
ifconfig -a
If as you say it only gives you 127.0.0.1 then there are two options:
Your network card is not attached or not recognized by the system
Your network DHCP server is not runnning or not connected
after that
add your ip address to your project's setting.py file
in my case my ip is 192.168.1.24
ALLOWED_HOSTS = ['192.168.1.24', '127.0.0.1', 'localhost']
then run your project using
python manage.py runserver 192.168.1.24:8000
now you can access your project to other devices :)
very simple,
first you need to add ip to allowed host,
ALLOWED_HOST =['*']
2. then execute python manage.py runserver 0.0.0.0:8000
now you can access the local project on different system in the same network

Making django server accessible in LAN

I have installed Django server and it can be accessed as below
http://localhost:8000/get-sms/
http://127.0.0.1:8000/get-sms/
suppose My IP is x.x.x.x .
From another PC under the same network when I do
my-ip:8000/get-sms/
but it is not working.
I can easily ping my IP with that computer.
Moreover, on my port 81, I have apache, which is easily accessible like below
http:///my-ip:81
What can be the issue? Do I need something extra in Django
Running the Django Development Server
This is what you're looking for. To help you further, here is what you should do:
python manage.py runserver 0.0.0.0:8000
By the way, this may be a duplicate of this question.
Here is what the documentation says:
Note that the default IP address, 127.0.0.1, is not accessible from
other machines on your network. To make your development server
viewable to other machines on the network, use its own IP address
(e.g. 192.168.2.1) or 0.0.0.0.
To add to #Depado 's answer you may need to add your LAN IP address to ALLOWED_HOSTS in the settings.py along with localhost. it would look like,
ALLOWED_HOSTS = ["localhost", "192.168.8.160"]
(if localhost isn't working use 127.0.0.1 as suggested by #Sabito 錆兎)
You can use https://ngrok.com/ this will expose your local web server to the internet/public.
Everywhere I looked, I kept seeing the answer to use the terminal command:
python manage.py runserver 0.0.0.0:8000
That works, but not if you want to run a remote debugger across the LAN (in my case VSCode), which launches the server automatically without a chance to modify the host ip address. However, I found a permanent solution:
Open: ./env/lib/python3.8/site-packages/django/core/management/commands/runserver.py
Search for: self.addr = ''
Replace '' with '0' and save. ('0' is shorthand for '0.0.0.0')
Now if you run: python manage.py runserver it is open to the local network, outputting: Starting development server at http://0:8000/
Importantly, the debugger now launches the server at http://0:8000/
If you haven't already, remember to add your client to allowed hosts in settings.py: ALLOWED_HOSTS = ["*"]
Blockquote

Why can’t Internet Explorer access my Django Development Server that’s accessible externally (i.e. not on localhost?)

I've been tasked by my programming teacher to learn Django. However, I need to code on a command line linux box and therefore cannot access the web page from said linux box.
To test, I need to host it on the local IP of the computer rather than localhost. I’m currently running the Django development server using this command:
python manage.py runserver 0.0.0.0:8000
Firefox and Chrome can access the development server at the Linux box’s IP address, but Internet Explorer can’t.
Unfortunately (I am said teacher), I can't provide a linux host - only a linux server and a windows host to do development on.
He's actually running the web server correctly, with the command provided by Paul. But for some reason, it's only visible in Firefox/Chrome, IE just provides a generic "Webpage cannot be displayed"
But this problem can be solved by using the entire address: http:// isn't optional like it is in Chrome and Firefox.
When you run the development server, you can make it available on the local network by specifying the IP address 0.0.0.0 for it, like this:
python manage.py runserver 0.0.0.0:8000
If your Linux box’s local IP address is e.g. 192.168.1.37, you’d access the development server at:
http://192.168.1.37:8000
Setup nginx to listen on a public port and proxy all of the requests to the django runserver.
You need to specify the IP clearly in the command line like this in order to let other clients to access. You also have to enable the port 8000 in the firewall:
python manage.py runserver 192.168.1.37:8000

Categories

Resources