Write a simple HTTP server with Python3

Source: Internet
Author: User

# written by vameiimport socket# addresshost = ' PORT = 8000# Prepare HTTP responsetext_content = ' http/1.x OK Cont Ent-type:text/html

Initial 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.