Web Static Server

Source: Internet
Author: User
From socket Import * from multiprocessing import Process import re def fun (client_socket): DocumentRoot = './html ' Client_data = CLIENT_SOCKET.RECV (1024) Client_data_lines = Client_data.splitlines () for line in Client_data_lin Es:print (line) Httprequestmethodline = Client_data_lines[0].decode (' utf-8 ') GetFileName = Re.match ("[^/] 
        + (/[^]*) ", Httprequestmethodline). Group (1) print ('-------------filename =%s '%getfilename) if GetFileName = = '/': GetFileName = documentroot + '/index.html ' else:getfilename = documentroot + getfilename print (  ' filename is%s '-----'%getfilename ' try:f = open (getfilename) except Ioerror:responseheaderlines = ' http/1.1 404 Not found\r\n ' responseheaderlines + = ' \ r \ n ' responsebody = ' Sorry file not found ' El Se:responseheaderlines = ' http/1.1 ok\r\n ' responseheaderlines + = ' \ r \ n ' responsebody = F.rea
  D () F.close ()  Finally:response = Responseheaderlines + responsebody client_socket.send (response.encode (' Utf-8 ')) Client_socket.close () if __name__ = = ' __main__ ': server_socket = socket (af_inet,sock_stream) Server_socket.s Etsockopt (sol_socket,so_reuseaddr,1) Addr = (', 8009) server_socket.bind (ADDR) Server_socket.listen () Whil
        E true:client,c_addr = server_socket.accept () p = Process (target=fun,args= (client,)) P.start ()

 Client.close ()

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.