P.41 03-01 SimpleHTTPServer ã使ã
æ¨æºã©ã¤ãã©ãªã«å«ã¾ãã SimpleHTTPServer ã使ã£ã¦ç°¡å㪠Web ãµã¼ããä½ãã¾ãã
SimpleHTTPServer ã¯ãã¡ã¤ã«ãç»åãæ±ãæ©è½ããæã£ã¦ãã¾ãã
- http://docs.python.org/lib/module-SimpleHTTPServer.html
- http://www.python.jp/doc/release/lib/module-SimpleHTTPServer.html
ã¤ã³ã¿ã©ã¯ãã£ãã·ã§ã«ã使ã£ã¦ Web ãµã¼ããåããã¦ã¿ã
- ã¤ã³ã¿ã©ã¯ãã£ãã·ã§ã«ãèµ·ã¡ä¸ãã
- ã³ã¼ããå
¥åãã
>>> import SimpleHTTPServer
>>> SimpleHTTPServer.test() - Web ãµã¼ããç«ã¡ä¸ãã
>>> import SimpleHTTPServer
>>> SimpleHTTPServer.test()
Serving HTTP on 0.0.0.0 port 8000 ... - ãã©ã¦ã¶ã§ http://127.0.0.1:8000/ ã¸ã¢ã¯ã»ã¹
ï¼ã¤ã³ã¿ã©ã¯ãã£ãã·ã§ã«ãç«ã¡ä¸ããéã®ä½æ¥ãã£ã¬ã¯ããªã®ãã¡ã¤ã«ä¸è¦§ã表示ãããï¼ - ã¡ãã»ã¼ã¸ãåºåããã
>>> import SimpleHTTPServer
>>> SimpleHTTPServer.test()
Serving HTTP on 0.0.0.0 port 8000 ...
localhost - - [11/Dec/2007 01:25:48] "GET / HTTP/1.1" 200 -
localhost - - [11/Dec/2007 01:25:49] code 404, message File not found
localhost - - [11/Dec/2007 01:25:49] "GET /favicon.ico HTTP/1.1" 404 -
æå¾ã®1è¡ãfavicon ãåå¾ãããã¨ãã¦ãã¨ããï¼"Get /favicon ãï¼ä»¥å¤ã¯æ¬ã®éãã§ããã
- Ctrl + c ã§ãµã¼ãã忢
- ãªãããã©ãã©ã¨åºåããã¦ã¢ã»ã
Traceback (most recent call last):
File "", line 1, in
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/SimpleHTTPServer.py", line 214, in test
BaseHTTPServer.test(HandlerClass, ServerClass)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/BaseHTTPServer.py", line 574, in test
httpd.serve_forever()
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/SocketServer.py", line 201, in serve_forever
self.handle_request()
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/SocketServer.py", line 217, in handle_request
request, client_address = self.get_request()
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/SocketServer.py", line 374, in get_request
return self.socket.accept()
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/socket.py", line 167, in accept
sock, addr = self._sock.accept()
KeyboardInterrupt
Python ã¹ã¯ãªãããHTML ãã¡ã¤ã«ãæ±ã
- P.43 å³03 ã¨åæ§ã«ãpython ã¹ã¯ãªãããã¡ã¤ã«ã¨ HTML ãã¡ã¤ã«ã使ãã
- simpleserver.py
import simpleHTTPServer
SimpleHTTPServer.test() - index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>SimpleHTTPServer</title>
</head>
<body>
<p>Python is awesome !</p>
</body>
</html>
- simpleserver.py
- ã¿ã¼ããã«ã§ä½æãããã¡ã¤ã«ã®ãããã£ã¬ã¯ããªãã«ã¬ã³ããã£ã¬ã¯ããªã¸ç§»åãã
- ã³ãã³ããå
¥åãã¦ãµã¼ããèµ·ã¡ä¸ãã
$ python simpleserver.py
- ã¨ã©ã¼ ï¼ï¼
ãsimpleHTTPServer ãªãã¦ååã®ã¢ã¸ã¥ã¼ã«ã¯ç¡ãããã£ã¦è¨ããã¾ãããæåã®ãsãã¯å¤§æåã®ãSãTraceback (most recent call last):
File "simpleserver.py", line 1, in
import simpleHTTPServer
ImportError: No module named simpleHTTPServer - python ã¹ã¯ãªãããä¿®æ£
- simpleserver.py
import SimpleHTTPServer
SimpleHTTPServer.test()
- simpleserver.py
- ï¼æ°ãåãç´ãã¦ãï¼ã³ãã³ããå
¥åãã¦ãµã¼ããèµ·ã¡ä¸ãã
$ python simpleserver.py
- ï¼ç¡äºãï¼Web ãµã¼ããèµ·ã¡ä¸ãã
$ python simpleserver.py
Serving HTTP on 0.0.0.0 port 8000 ... - ãã©ã¦ã¶ã§ http://127.0.0.1:8000/ ã¸ã¢ã¯ã»ã¹
- ãPython is awesome !ã
仿¥ã¯ããã¾ã§ã
åç
- ã¢ã¸ã¥ã¼ã«åãééããªãï¼åç´ãª typo ãç¡ããï¼
- ãªã¹ãä¸ã«ã³ã¼ããæ¸ãã®ã大å¤ï¼ã¦ãã¯ã¦ãªã«ä½ãæ¸ãæãã¤ããããªäºè¨ã£ã¦ãæ°ãããï¼
- 1æ¥ã®æ¥è¨åéãå¢ããããã©ã¦ã¶ã®åä½ãç·©æ ¢ã«ãªã£ã¦å°ãï¼å°ãªãã§ï¼
ããã¯ã¢ãããã¼ã¿ãä¿åãããã¼ã«ã«è¨æ¶é åãä¸è¶³ãã¦ãã¾ããããã¯ã¢ããã®ä¿å容éè¨å®ã§æå¤§å®¹éãå¢ããã¦ãã ããã
ã¨ã表示ããã¦ããåãã¦è¦ããããã¯ã¢ãããªãã¦ããã¦ããã