Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.53 KB | None | 0 0
  1. import http.server
  2. import socketserver
  3. import time
  4.  
  5. # port serwera
  6. PORT = 8000
  7.  
  8.  
  9. # dziedziczymy po klasie handlera bazowego
  10. class MyHandler(http.server.BaseHTTPRequestHandler):
  11.     def do_HEAD(self):
  12.         ''' metoda obsługuje zapytania HEAD'''
  13.         self.send_response(200)
  14.         self.send_header("Content-type", "text/html")
  15.         self.end_headers()
  16.  
  17.     def do_GET(self):
  18.         ''' metoda obsługuje zapytania GET '''
  19.         print('przyszło zapytanie:', self.path)
  20.  
  21.         # rozwiązywanie zapytania
  22.         if self.path == '/' or self.path.endswith('index.html'):
  23.             # wysyłanie nagłówka
  24.             self.send_response(200)
  25.             self.send_header("Content-type", "text/html")
  26.             self.end_headers()
  27.             content = '''
  28.            <html>
  29.                <head>
  30.                    <title>zegar</title>
  31.                    <link rel="stylesheet" type="text/css" href="style.css">
  32.                        <head>
  33.                        <body>
  34.                    <div>zegar: %s</div>
  35.                    <script src="script.js"></script>
  36.                </body>
  37.            </html>'''
  38.             # wstawianie stringa
  39.             content %= time.strftime('%H:%M:%S')
  40.             # wysyłanie zawartości strony
  41.             self.wfile.write(bytes(content, 'utf-8'))
  42.             # wyjście z metody (żeby dalszy kod się nie wykonywał
  43.             return
  44.         # obsługa np. cssów
  45.         if self.path.endswith('.css'):
  46.             self.send_response(200)
  47.             # content type jest inny
  48.             self.send_header("Content-type", "text/css")
  49.             self.end_headers()
  50.             # otwieranie pliku w trybie binarnym
  51.             with open('.' + self.path, 'rb') as f:
  52.                 self.wfile.write(f.read())
  53.             return
  54.         # obsługa javascript
  55.         if self.path.endswith('.js'):
  56.             self.send_response(200)
  57.             # content type jest inny
  58.             self.send_header("Content-type", "application/javascript")
  59.             self.end_headers()
  60.             # otwieranie pliku w trybie binarnym
  61.             with open('.' + self.path, 'rb') as f:
  62.                 self.wfile.write(f.read())
  63.             return
  64.  
  65.         # analogicznie inne typy plików
  66.  
  67.         # w przypadku błędów
  68.         self.send_error(404, 'Strona nieodnaleziona')
  69.  
  70.  
  71. def main():
  72.     handler = MyHandler
  73.     address = ('', PORT)
  74.     s = socketserver.TCPServer(address, handler)
  75.     print('serwer nasłuchuje na porcie:', PORT)
  76.     s.serve_forever()
  77.  
  78. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement