# written by vameiimport socket# addresshost = ' PORT = 8000# Prepare HTTP responsetext_content = ' http/1.x OK Cont Ent-type:text/htmlInitial code from http://www.cnblogs.com/vamei/archive/2012/10/30/2744955.html
The original code is estimated to run in the Python2, run an error in the Python3, most of the error due to type conversion, the general meaning is that the bytes type cannot be directly added to the STR type, so we need to add. Encode () and. Decode () in the key. The other main contents are from Vamei, and the actual measurement is available.
The process of the entire request is the client access 127.0.0.1:8000, after the server responds to the connection, according to the resolution method for ' GET ', sent to the client ready Text_content, after sending the connection disconnects, The client browser parses the string content in HTML, finds a link to the root file directory of the Test.jpg, and then again to the server to request/test.jpg content, the service side, know method is/ After the test.jpg, will be ready for the picture content Plus file hair sent past, send the connection immediately interrupted.
So HTTP is actually a variant of TCP, first to establish a long connection, and then a client request data, the service side according to the other side of the request to send the corresponding data, immediately after the end of the break. and the text socket. The Sock_stream attribute, in fact, refers to the TCP.
Write a simple HTTP server with Python3