Develop a simple Web server yourself

Source: Internet
Author: User

Use Python to implement a simple version of the Web server, designed to understand how the Web server works and understand the HTTP protocol. Does not involve multithreading, concurrency and other content, and then special study. First, the code, and then explain later.

# Coding=utf-8ImportSocket class httpd(object):         def __init__(self,host,port):Self.host = Host Self.port = port def parse_info(self,data):                Global_env _env = {} lines = Data.splitlines ()ifLen (lines) >0: info = lines[0].split () _env["Method"] = info[0] _env["uri"] = info[1]dellines[0] forIinchLines:offset = I.index (":") _env[i[0: offset]] = i[offset+1::] def log(self,data):f = open ("Access.log","a") f.write (data) f.close () def start(self):                Print "Python Web server Starring...\nlisten at%s"% Self.port httpd = Socket.socket (socket.af_inet,socket. SOCK_STREAM) httpd.setsockopt (socket. Sol_socket,socket. SO_REUSEADDR,1) Httpd.bind ((Self.host,self.port)) Httpd.listen (5) while True: Conn,addr = httpd.accept () Request_data = CONN.RECV (1024x768) Self.log (request_data) Self.parse_info (Request_data.strip ()) BODY ="http/1.1 Ok\ncontent-type:text/html;charset=utf-8\npower:python web\nauther:yagas\n\n"Body + ="Conn.sendall (body) conn.close ()if__name__ = ="__main__": Server = httpd ("0.0.0.0", the) Server.start ()

When we access through the browser, we can see the "Simple Web server Instance" page, and recorded the access to the time of the received header data, as follows:

GET / http/1.1Host:localhostConnection:keep-aliveCache-control:max-age=0Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8user-agent:mozilla/5.0 (Windows NT 6.3; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/43.0.2357.81 safari/537.36accept-encoding:gzip, deflate, SDCHAccept-language:zh-cn,zh;q=0.8GET /favicon.ico http/1.1Host:localhostConnection:keep-alivePragma:No-cacheCache-control:No-cacheuser-agent:mozilla/5.0 (Windows NT 6.3; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/43.0.2357.81 safari/537.36Accept:*/*Referer:http://localhost/accept-encoding:gzip, deflate, SDCHAccept-language:zh-cn,zh;q=0.8

The primary message is the first line:

GET / HTML1.1
GET /favicon.ico HTTP/1.1

The requested method, the request address, the requested protocol . Also some browsers send the source host, browser information, language information, cache information and so on.
The server receives the request and processes it, and then returns the data in the form of a header, completing the data requests and responses.

HTTP/1.1200OKContent-Type:text/html;Charset=utf-8数据主体信息

The submission structure of the response information is: The response protocol status OK, an empty line, and then the output HTML content .
When we get to the information, we can implement a simple Web server for the requested address, as long as the corresponding data is returned, regardless of the programming language used, Ruby, Perl, C, go.
This simple Web server can be embedded in a system service, to provide users with a web interface to manage, such as the management interface of the router and so on.
This is just a way of thinking for everyone. Technical things, it depends on how you use.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Develop a simple Web server yourself

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.