http - Julia:如何让 HTTP.jl 从 WSL2 VM 的 ip 服务?
问题描述
我在 WSL2 上启动了一个简单的 http 服务器,以在 localhost:8081 提供简单的 HTML 页面。
我想通过主机上的 localhost:8081 (或任何 URL)访问它。
我按照说明https://docs.microsoft.com/en-us/windows/wsl/compare-versions。
我曾经ip addr | grep eth0
在inet下找到IP,然后我用Python和Julia启动了一个简单的HTTP服务器
import http.server
import socketserver
PORT = 8000
Handler = http.server.SimpleHTTPRequestHandler
httpd = socketserver.TCPServer(("", PORT), Handler)
print("serving at port", PORT)
httpd.serve_forever()
上面的 python 版本可以正常工作,但 Julia 服务器无法正常工作。
using HTTP
using HTTP: Sockets, @ip_str
HTTP.serve() do request::HTTP.Request
@show request
@show request.method
@show HTTP.header(request, "Content-Type")
@show HTTP.payload(request)
try
return HTTP.Response("Hello")
catch e
return HTTP.Response(404, "Error: $e")
end
end
它为 HTTP 流量打开端口 8000 和 8081。然后我去了主机并做localhost:8081
了$WSL2VMIP:8081
都没有奏效。
解决方案
对于 Julia,您似乎需要提供 WSL2 VM 的 IP。使用获取 IPip addr | grep eth0
并查找类似 IP172.69.13.20/20
并设置myip = ip"172.69.13.20"
请注意,使用ip"0.0.0.0"
很方便,但可能不安全(例如在公共咖啡馆),因此请小心使用。
using HTTP
using HTTP: @ip_str
HTTP.serve(myip) do request::HTTP.Request
@show request
@show request.method
@show HTTP.header(request, "Content-Type")
@show HTTP.payload(request)
try
return HTTP.Response("Hello")
catch e
return HTTP.Response(404, "Error: $e")
end
end
推荐阅读
- java - 停止在 Android Studio 中突出显示方法参数名称
- aframe - 如何使用 A-Frame 将对象带到控制器
- curl - 对内部网站的 curl 请求
- php - Laravel 5.5如何修改登录过程以使用existant db用户表(SQL Server)
- python - 在 C++ 中使用加载的张量流模型运行推理
- php - 动态和依赖形式
- java - 按名字和姓氏对多维字符串数组进行排序
- java - 尝试在 .Adapter.ListSourceAdap 的空对象引用上调用虚拟方法 Model.WebSite.getSources()'
- php - PHP 根据 id 显示来自 MySQL 查询的行并按日期分组
- r - 当数据有汉字时,ggplot2会导致Rstudio崩溃