简体   繁体   English

如何使用 WSL(适用于 Linux 的 Windows 子系统)访问 Django 服务器?

[英]How can I access the Django server using WSL (Windows Subsystem for Linux)?

I'm using Bash on Ubuntu on Windows 10 and installed django within a python virtual environment.我在 Windows 10 上的 Ubuntu 上使用 Bash,并在 python 虚拟环境中安装了 django。 I'm able to start the django server successfully, but when I type in " http://127.0.0.1:8000/ " in my windows 10 browser I don't get a response.我能够成功启动 django 服务器,但是当我在 Windows 10 浏览器中输入“ http://127.0.0.1:8000/ ”时,我没有得到响应。

I've tried changing the port number to something else (eg. python3 manage.py runserver 127.0.0.1:7171) and I still get nothing.我尝试将端口号更改为其他内容(例如 python3 manage.py runserver 127.0.0.1:7171),但我仍然一无所获。

I don't think it's a firewall issue, as WSL and the browser and all running on the same Windows 10 laptop.我不认为这是防火墙问题,因为 WSL 和浏览器都在同一台 Windows 10 笔记本电脑上运行。 Do you know what the issue might be?你知道可能是什么问题吗?

(env) $ python3 manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
July 26, 2018 - 00:50:25
Django version 2.0.7, using settings 'portfolio.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Windows Version: Windows 10 build 1709 (16299.492) Windows 版本:Windows 10 build 1709 (16299.492)

Ubuntu Version: Ubuntu 16.04.5 LTS Ubuntu 版本:Ubuntu 16.04.5 LTS

On your WSL terminal run:在您的 WSL 终端上运行:

python3 manage.py runserver 0.0.0.0:8080

Include your hostname as Allowed Host(Computer Name) in settings.py .settings.py 中包含您的主机名作为允许的主机(计算机名)。

In your browser run: http://yourhostname:8080 .在浏览器中运行: http://yourhostname:8080

When doing this myself a while back, I found there was no way to bind things running in WSL to localhost.不久前自己这样做时,我发现无法将在 WSL 中运行的东西绑定到本地主机。

I did however have success when connecting via my hostname.然而,通过我的主机名连接时我确实成功了。

So, given the hostname GAMMAFLYER, do the following:因此,给定主机名 GAMMAFLYER,请执行以下操作:

python3 manage.py runserver 0.0.0.0:8000

This allows external connections.这允许外部连接。

Then in your browser:然后在您的浏览器中:

http://GAMMAFLYER:8000

I have the same issue and answer of @Shadow works for me well, ie I have ability to access http://HOSTNAME:8000 from windows after python manage.py runserver 0.0.0.0:8000 on WSL.我有同样的问题,@Shadow 的答案对我很有效,即我有能力在 WSL 上的python manage.py runserver 0.0.0.0:8000之后从 Windows 访问http://HOSTNAME:8000

But there is a way to get it work usual way and access http://localhost:8000 on windows by disabling a VirtualBox network adapter for those who have VirtualBox installed on their Windows systems.但是有一种方法可以通过为在 Windows 系统上安装 VirtualBox 的人禁用 VirtualBox 网络适配器来使其正常工作并在 Windows 上访问http://localhost:8000

I have found that the IP address of my hostname on windows is the IP of VirtualBox Host-Only Ethernet Adapter :我发现我的主机名在 Windows 上的 IP 地址是VirtualBox Host-Only Ethernet Adapter的 IP:

(windows cmd): (Windows cmd):

ping -4 HOSTNAME

Pinging HOSTNAME [192.168.56.1] with 32 bytes of data:
Reply from 192.168.56.1: bytes=32 time<1ms TTL=128
Reply from 192.168.56.1: bytes=32 time<1ms TTL=128

ipconfig -all

Ethernet adapter VirtualBox Host-Only Network:

   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : VirtualBox Host-Only Ethernet Adapter
   Physical Address. . . . . . . . . : ***
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes
   Link-local IPv6 Address . . . . . : fe80::7d75:46fe:41d8:1ee3%5(Preferred)
   IPv4 Address. . . . . . . . . . . : 192.168.56.1(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . :
   DHCPv6 IAID . . . . . . . . . . . : ***
   DHCPv6 Client DUID. . . . . . . . : ***
   DNS Servers . . . . . . . . . . . : fec0:0:0:ffff::1%1
                                       fec0:0:0:ffff::2%1
                                       fec0:0:0:ffff::3%1
   NetBIOS over Tcpip. . . . . . . . : Enabled

After I have disabled this adapter from Control Panel\\Network and Internet\\Network Connections and reran python manage.py runserver on WSL shell it worked well and I have ability to access http://localhost:8000 from windows.Control Panel\\Network and Internet\\Network Connections禁用此适配器并在 WSL shell 上重新运行python manage.py runserver ,它运行良好,我可以从 Windows 访问http://localhost:8000

I am currently running wsl2 on a windows 10 box.我目前正在 Windows 10 机器上运行 wsl2。 I ran into this issue when trying to setup a django project.我在尝试设置 django 项目时遇到了这个问题。 I was able to get it working by running the following command, on powershell.我能够通过在 powershell 上运行以下命令来使其工作。

netsh interface portproxy reset

There were some config errors with my proxyport and the above command was able to fix those errors.我的代理端口存在一些配置错误,上面的命令能够修复这些错误。 So if you had wsl2 working at one point and now it's not working, run that command.因此,如果您曾让 wsl2 工作,但现在无法工作,请运行该命令。 However, if you just installed wsl2 then you need to add a proxy port in order to access the port locally : netsh interface portproxy add v4tov4 listenport=4000 listenaddress=0.0.0.0 connectport=4000 connectaddress=192.168.101.100但是,如果您刚刚安装了 wsl2,那么您需要添加一个代理端口才能在本地访问该端口netsh interface portproxy add v4tov4 listenport=4000 listenaddress=0.0.0.0 connectport=4000 connectaddress=192.168.101.100

In this case I am exposing the 4000 port.在这种情况下,我暴露了4000端口。

Source:来源:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用python在wsl(Linux的Windows子系统)上打开和运行一些命令? - How to open and run some commands on wsl (Windows Subsystem for Linux) by using python? Linux (WSL 2) 和 Jupyter Lab 的 Windows 子系统和 Jupyter Lab:如何打开保存在 Linux 文件系统中的 Jupyter Notebook? - Windows Subsystem for Linux (WSL 2) and Jupyter Lab : How to open a Jupyter Notebook saved at the Linux file system? 将 Jupyter 与适用于 Linux 的 Windows 子系统结合使用 - Using Jupyter with Windows Subsystem for Linux Visual Studio代码,使用WSL进行Python linting(适用于Linux的Windows子系统) - Visual Studio Code, Python linting with WSL (Windows Subsystem for Linux) Crontab 未在 Windows Subsystem for Linux (WSL) 上运行 python 脚本 - Crontab not running python script on Windows Subsystem for Linux (WSL) 将 ssh 连接到我的服务器后,如何将 python 文件从我的 Windows 复制到 Linux 子系统? - How do i copy a python file from my windows to Linux subsystem after connect ssh to my server? Bitbake 服务器无法在适用于 Linux 的 Windows 子系统上启动 - Bitbake Server does not start on Windows Subsystem for Linux 如何为Windows子系统Linux转换Windows路径 - How to translate a Windows path for Windows Subsystem for Linux 如何在 Linux 的 Windows 子系统中安装 Python 模块? - How to install Python modules in Windows subsystem for Linux? 如何使用Windows对服务器进行Django远程访问 - How to Django remote access to server using Windows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM