標籤:
# coding=utf-8'''Created on 2014年6月15日@author: Yang'''import socketimport datetime# 初始化sockets = socket.socket()# 擷取主機名稱, 也能夠使用localhost# host = socket.gethostname()host = "localhost"# 預設的http協議連接埠號碼port = 80 # 綁定serversocket的ip和連接埠號碼s.bind((host, port)) # server名字/版本server_name = "MyServerDemo/0.1" # 緩衝時間, 緩衝一天expires = datetime.timedelta(days=1)# GMT時間格式GMT_FORMAT = '%a, %d %b %Y %H:%M:%S GMT'# 對應網頁的內容content = '''<html><head><title>MyServerDemo/0.1</title></head><body><h1>coming soon</h1></body></html>''' # 可同一時候串連五個clients.listen(5) # server迴圈while True: # 等待client串連 c, addr = s.accept() print 'Got connection ', addr, '\n' # 顯示請求資訊 print '--Request Header:' # 接收瀏覽器的請求, 不作處理 data = c.recv(1024) print data # 獲得請求的時間 now = datetime.datetime.utcnow() # 對應標頭檔和內容 response = '''HTTP/1.1 200 OKServer: %sDate: %sExpires: %sContent-Type: text/html;charset=utf8Content-Length: %sConnection: keep-alive %s''' % (server_name,now.strftime(GMT_FORMAT),(now + expires).strftime(GMT_FORMAT),len(content),content) # 發送回應 c.send(response) print '--Response:\r\n', response c.close()
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcG9tbWVfcWl4aWFvaHU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" >
【Python】 做一個簡單的 http server