How to access the local Django webserver from outside world

前端 未结 9 1679
天命终不由人
天命终不由人 2020-12-02 04:06

I followed the instructions here to run Django using the built-in webserver and was able to successfully run it using python manage.py runserver. If I access 1

相关标签:
9条回答
  • 2020-12-02 04:30

    Pick one or more from:

    • Your application isn't successfully listening on the intended IP:PORT
      • Because you haven't configured it successfully
      • Because the user doesn't have permission to
    • Your application is listening successfully on the intended IP:PORT, but clients can't reach it because
      • The server local iptables prevents it.
      • A firewall prevents it.

    So, you can check that your application is listening successfully by running lsof -i as root on the machine and look for a python entry with the corresponding port you've specified.

    Non-root users generally cannot bind to ports < 1024.

    You'll need to look at iptables -nvL to see if there's a rule that would prevent access to the ip:port that you are trying to bind your application to.

    If there is an upstream firewall and you don't know much about it, you'll need to talk to your network administrators.

    0 讨论(0)
  • 2020-12-02 04:30

    UPDATED 2020 TRY THIS WAY

    python manage.py runserver yourIp:8000
    
    ALLOWED_HOSTS = ["*"]
    
    0 讨论(0)
  • 2020-12-02 04:31

    You have to run the development server such that it listens on the interface to your network.

    E.g.

    python manage.py runserver 0.0.0.0:8000
    

    listens on every interface on port 8000.

    It doesn't matter whether you access the webserver with the IP or the hostname. I guess you are still in your own LAN.
    If you really want to access the server from outside, you also have to configure your router to forward port e.g. 8000 to your server.


    Check your firewall on your server whether incoming connections to the port in use are allowed!

    Assuming you can access your Apache server from the outside successfully, you can also try this:

    • Stop the Apache server, so that port 80 is free.
    • Start the development server with sudo python manage.py runserver 0.0.0.0:80
    0 讨论(0)
  • 2020-12-02 04:32

    I had to add this line to settings.py in order to make it work (otherwise it showed an error when accessed from another computer)

    ALLOWED_HOSTS = ['*']
    

    then ran the server with:

    python manage.py runserver 0.0.0.0:9595
    

    Also ensure that the firewall allows connections to that port

    0 讨论(0)
  • 2020-12-02 04:32

    I'm going to add this here:

    1. sudo python manage.py runserver 80

    2. Go to your phone or computer and enter your computers internal IP (e.g 192.168.0.12) into the browser.

    At this point you should be connected to the Django server.

    This should also work without sudo:

    python manage.py runserver 0.0.0.0:8000
    
    0 讨论(0)
  • 2020-12-02 04:36

    If you are using Docker you need to make sure ports are exposed as well

    0 讨论(0)
提交回复
热议问题