2

Is there an easy way to make an HTTP request over an existing socket in Python?

I've seen this answer, which amounts to overriding http.client.HTTPConnection and could be adapted to my needs, but it seems to me there must be an easier way.

In my particular case, I want to make a request over an SSH channel, not a socket, but the interface is the same enough that they should be indistinguishable.

3
  • What about solving this with ssh port forwarding, in which case you wouldn't need any changes to http.client? Commented Sep 26, 2017 at 16:56
  • Also see stackoverflow.com/questions/14665064/…, which is almost a duplicate except it involves requests rather than http.client. Commented Sep 26, 2017 at 16:58
  • @larsks Yes, I've considered just doing port forwarding. However, this is part of a somewhat bigger system that already uses paramiko to open an SSH session to the server. I could the set up port forwarding on that session and connect to the local port; but that would then mean I have a python thread that opens a socket that feeds another python thread that copies data into the SSH channel. It all seems a bit round-a-bout. Commented Sep 26, 2017 at 17:36

2 Answers 2

1

See this answer which shows how to use the requests library over an open socket or SSH tunnel.

Sign up to request clarification or add additional context in comments.

Comments

-1

You can send GET /link/to/url HTTP/1.0\n\n over the socket and read back the response. This however requires you to do any and all HTTP processing yourself, which is probably not really what you want to do. Injecting the socket into a higher level library is the better choice.

1 Comment

Yes, thanks, I know how to write a raw HTTP GET request. Writing a raw multi-part POST is beyond me, though, and it seems there should be a library for that...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.