Julia: How to make HTTP.jl serve from WSL2 VM's ip?

Viewed 271

I started a simple http server on WSL2 to serve simple HTML page at localhost:8081.

I would like to have access to this via localhost:8081 (or whatever the URL is) on the host machine.

I followed the instructions https://docs.microsoft.com/en-us/windows/wsl/compare-versions.

I used ip addr | grep eth0 to find the IP under inet and then I started a simple HTTP server in Python and Julia

import http.server
import socketserver

PORT = 8000

Handler = http.server.SimpleHTTPRequestHandler

httpd = socketserver.TCPServer(("", PORT), Handler)

print("serving at port", PORT)
httpd.serve_forever()

the python version above works with no issues but the Julia server just doesn't work.

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

which opens port 8000 and 8081 for HTTP traffic. Then I went to the host machine and did localhost:8081 and $WSL2VMIP:8081

Neither worked.

1 Answers

For Julia, you seem to need to provide the IP of the WSL2 VM. Get the IP using ip addr | grep eth0 and look for IP like 172.69.13.20/20 and set myip = ip"172.69.13.20"

Note that using ip"0.0.0.0" is convenient but it may be INSECURE (e.g. at a public cafe) so use with care.

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
Related