![]() |
Tutorial How to modify an http server to work over https - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: Python (https://sinister.ly/Forum-Python) +--- Thread: Tutorial How to modify an http server to work over https (/Thread-Tutorial-How-to-modify-an-http-server-to-work-over-https) |
How to modify an http server to work over https - 3SidedSquare - 04-05-2013 Alright, so I spent 8 hours over the last 2 days ripping out my hair, facial hair, arm hair, leg hair, pubic hair, ect. trying to figure out how the hell to make a socket work with https. I'm writing this in the hopes that will will save someone a lot of time, pain, and hair. First, be able to work with http before trying https! Http see thread To work with https, you're going to new a few more things. Namely, a certificate, and a keyfile. I generated one using this site. (Obviously, the certificate will not be verified by any trusted distributor, so clients will get a warning saying something along the lines of "This site has presented a security certificate that is not signed by a trusted source, do you want to continue?") Next, there's a handy part of the python library that helps do a lot of behind-the-scenes type stuff Code: import ssl Next, we need to wrap our socket into an SSLSocket Code: ... Create a regular socket with the name bindsocket ... Now that our socket is wrapped as an SSLSocket, we can start reading and writing (Remember sockets are bi-directional!). SSLSockets are StreamSockets, which is a problem because http is very un-stream like. To work with https, we need files that we read from, to get the user's request; and a socket to write to, so we can serve the request. Code: ... Now that we've gotten all the data that we want, and have all the data we want to send ready to send, all that's left is to send it! Code: ... RE: How to modify an http server to work over https - Charon - 04-05-2013 Good tutorial 3SidedSquare, this will definitely help people who want to work with HTTPS. |