jeudi 30 juillet 2020

Send HTTP Response in Python with socket Library on Heroku?

I'm trying to get a Heroku web app to send a proper HTTP response to a browser. I'm also trying to do this using only the socket library. Right now, I've been able to read HTTP requests, but every attempt to respond to them has ended in something like

sock=backend at=error code=H18 desc="Server Request Interrupted"
method=GET path="/favicon.ico" host=appame.herokuapp.com
request_id=a6640486-2c72-429d-a2a0-e79784bbc4d3 fwd="99.39.166.192"
dyno=web.1 connect=0ms service=0ms status=503 bytes=128 protocol=http

I've checked all over and believe the closest question to the one I have is this one, but the solution didn't work for me. Every request immediately returns with the above status code 503.

Here's the code I'm currently using to receive and then respond to incoming requests:

# imagine I've received the data already and am trying to respond.
resp_body = '<html><body>This is a test response.</body></html>'
resp_body_bytes = resp_body.encode('utf-8')
data_back = 'HTTP/1.1 200 OK\nContent-Type: text/html\nContent-Length: {0}\nConnection: close\n\n{1}\n'.format(len(resp_body_bytes),resp_body)
cli_sock.sendall(data_back.encode('utf-8'))
print('Sent data back:\n{0}'.format(data_back))
cli_sock.close()

That final print statement always executes without problems, but I'm still unable to field HTTP requests properly. Am I missing something obvious? I'm new to Heroku, so that might be the case.




Aucun commentaire:

Enregistrer un commentaire