Equivelent HTTP.jl code for Python socket implementation

I’m trying to emulate a piece of Python code that talks to a http server in Julia but I don’t really understand the python code or how to translate it into equivalent HTTP.jl and Sockets.jl code.

Here’s a very compressed MWE of the python code

import socket

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
IPAddress = socket.gethostbyname(socket.gethostname())
portNumber = 8083
server.bind((IPAddress, portNumber))
server.listen(1)
client, address = server.accept()
client.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
data = client.recv(65536)
print(data)
output = "HTTP/1.1 200 OK\r\nContent-Length: 2\r\nContent-type: application/json\r\n\r\n{}"
client.send(output.encode('utf-8'))

(The real code puts the data-receiver on a loop in a different thread)

I’m mostly wondering what the setsockopt and listen parts do and what their equivalents are in HTTP.jl and Sockets.jl are?

I’ve tried using HTTP.serve with the same IP and port like

datareceiver = HTTP.serve!(IPAddress, 8083, verbose=true) do req
	@show req
	return HTTP.Response("HTTP/1.1 200 OK\r\nContent-Length: 2\r\nContent-type: application/json\r\n\r\n{}")
end

but it doesn’t seem to receive any messages.

There was a problem with what I was sending to the server. The code with HTTP.serve! does in fact work.

1 Like