File tree Expand file tree Collapse file tree 4 files changed +46
-0
lines changed Expand file tree Collapse file tree 4 files changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+ Simple Python Web Server
2
+ ========================
3
+
4
+ This is the source code for howCode's simple Python Web Server.
5
+
6
+ You can watch the video that accompanies this source code here: https://youtu.be/hFNZ6kdBgO0
Original file line number Diff line number Diff line change
1
+ <!DOCTYPE html>
2
+ < html >
3
+ < head >
4
+ < meta charset ="utf-8 ">
5
+ < title > Hello world!</ title >
6
+ </ head >
7
+ < body >
8
+ < h1 > Index page!</ h1 >
9
+ </ body >
10
+ </ html >
Original file line number Diff line number Diff line change
1
+ from http .server import HTTPServer , BaseHTTPRequestHandler
2
+
3
+
4
+ class Serv (BaseHTTPRequestHandler ):
5
+
6
+ def do_GET (self ):
7
+ if self .path == '/' :
8
+ self .path = '/index.html'
9
+ try :
10
+ file_to_open = open (self .path [1 :]).read ()
11
+ self .send_response (200 )
12
+ except :
13
+ file_to_open = "File not found"
14
+ self .send_response (404 )
15
+ self .end_headers ()
16
+ self .wfile .write (bytes (file_to_open , 'utf-8' ))
17
+
18
+
19
+ httpd = HTTPServer (('localhost' , 8080 ), Serv )
20
+ httpd .serve_forever ()
Original file line number Diff line number Diff line change
1
+ <!DOCTYPE html>
2
+ < html >
3
+ < head >
4
+ < meta charset ="utf-8 ">
5
+ < title > Hello world!</ title >
6
+ </ head >
7
+ < body >
8
+ < h1 > Hello World!</ h1 >
9
+ </ body >
10
+ </ html >
You can’t perform that action at this time.
0 commit comments