LoginSignup
3
2

More than 1 year has passed since last update.

Pythonでサーバーを立てて画像を表示する

Posted at

はじめに

家のハムスターの様子を外でも見たいので、まずはサーバーを作って画像を表示できるようにしようと思います。

環境

windows10
python3.9.4
google chrome

サーバーの起動

適当なフォルダにindex.htmlとserver.py、表示したい画像を用意します。

server.py
import http.server
import socketserver

PORT = 8000
Handler = http.server.SimpleHTTPRequestHandler

with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print("serving at port", PORT)
    httpd.serve_forever()

index.html
<html>
<meta charset="utf-8">
<body>
<h1>ハムスター</h1>
<img src="あなたのハムスターの画像のパス.jpg"title="kinkuma" width="400" height="auto">
</body>
</html>


コマンドプロンプトを開いて作ったフォルダのあるディレクトリまで移動し、以下を入力します。

$ python server.py

成功していればserving at port 8000と表示されます。

開いてみる

http://localhost:8000/にアクセスします。
image.png
こんな感じで画像が表示されていると思います。

参考記事

pythonでローカルwebサーバを立ち上げる

3
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2