Python 如何在本地主机服务器上放置映像

Python 如何在本地主机服务器上放置映像,python,python-2.7,web-scraping,server,host,Python,Python 2.7,Web Scraping,Server,Host,我现在正在写一个程序,从互联网上抓取图像,用它们启动服务器并编写html 这是我的密码: import json import requests import urllib2 import webserver import bottle from bottle import run, route, request #get images r = requests.get("http://web.sfc.keio.ac.jp/~t14527jz/midterm/imagelist.json

我现在正在写一个程序,从互联网上抓取图像,用它们启动服务器并编写html

这是我的密码:

import json 
import requests  
import urllib2
import webserver
import bottle
from bottle import run, route, request

#get images
r = requests.get("http://web.sfc.keio.ac.jp/~t14527jz/midterm/imagelist.json")
r.text
imageli = json.loads(r.text)
i = 1
for image in imageli:
    if i <= 5:
        fname = str(i)+".png"
        urllib.urlretrieve(image,fname)
        i += 1

#to start a server
@route('/')
#write a HTML, here is where the problem is,
#the content of html is <image src= "1.png"> but the picture is not on the server. 
#and I don't know how to do it
def index():   
    return "<html><head><title>Final exam</title></head><body> <img src=\"1.png\"/>\n <img src=\"2.png\"/>\n <img src=\"3.png\"/>\n<img src=\"4.png\"/>\n<img src=\"5.png\"/>\n</body></html>"
if __name__ == '__main__':        
    bottle.debug(True) 
    run(host='localhost', port=8080, reloader=True)
导入json
导入请求
导入urllib2
导入Web服务器
进口瓶
从瓶子导入运行、路由、请求
#获取图像
r=请求。获取(“http://web.sfc.keio.ac.jp/~t14527jz/midterm/imagelist.json”)
r、 正文
imageli=json.loads(r.text)
i=1
对于imageli中的图像:

如果我这里的问题是您没有定义一个路由来为静态文件提供服务。通过添加以下路由,可以将根目录设置为提供静态文件:

# To serve static png files from root.
@bottle.get('/<filename:re:.*\.png>')
def images(filename):
    return bottle.static_file(filename, root='')
#从根目录提供静态png文件。
@瓶子。获取(“/”)
def图像(文件名):
返回瓶子.static_文件(文件名,根=“”)
但您确实应该将它们移动到一个子目录,如:

import json
import requests
import urllib
import bottle
from bottle import run, route, request

#get images
r = requests.get("http://web.sfc.keio.ac.jp/~t14527jz/midterm/imagelist.json")
r.text
imageli = json.loads(r.text)
i = 1
for image in imageli:
    if i <= 5:
        fname = "static/{}.png".format(i)  # Note update here
        urllib.urlretrieve(image, fname)
        i += 1

#to start a server
@route('/')
def index():
    return "<html><head><title>Final exam</title></head><body> <img src=\"1.png\"/>\n <img src=\"2.png\"/>\n <img src=\"3.png\"/>\n<img src=\"4.png\"/>\n<img src=\"5.png\"/>\n</body></html>"

# To serve static image files from static directory
@bottle.get('/<filename:re:.*\.(jpg|png|gif|ico)>')
def images(filename):
    return bottle.static_file(filename, root='static')

if __name__ == '__main__':
    bottle.debug(True)
    run(host='localhost', port=8080, reloader=True)
导入json
导入请求
导入URL库
进口瓶
从瓶子导入运行、路由、请求
#获取图像
r=请求。获取(“http://web.sfc.keio.ac.jp/~t14527jz/midterm/imagelist.json”)
r、 正文
imageli=json.loads(r.text)
i=1
对于imageli中的图像:

如果我是问题的下载或服务部分?请先将您遇到问题的代码减少到尽可能小的示例中。另请参见。您已将图像保存在本地,并且有一个瓶子路径,该路径提供一些链接到图像的HTML,但实际提供图像的代码在哪里?问题是,在服务器上,图像无法上载,因为它保存在本地。我刚刚上传了我的代码,我不知道那个评论是什么意思。我只是简单地指出,你似乎没有任何东西真正服务于你下载的图像。对不起,我将再次修改它