【发布时间】:2016-05-16 20:36:27
【问题描述】:
我目前正在尝试在一个非常基本的网站中加载几个 SVG 图像,但遇到了奇怪的问题。如果相关,我在绘制各种参数方程时使用由 matplotlib 创建的 SVG 文件。我尝试加载的文件只是加载四个随机 SVG 图像(当我尝试单独加载它们时,所有这些图像都可以正常工作并加载)并将它们放在一个小网页中:
<html>
<img src="graphs/{0}.svg">
<img src="graphs/{1}.svg">
<img src="graphs/{2}.svg">
<img src="graphs/{3}.svg">
</html>
({0} 到 {3} 被替换为实际文件名,例如 1070)。由于某种原因,这每次都失败。例如,
<html>
<img src="graphs/2286.svg">
<img src="graphs/7499.svg">
<img src="graphs/7444.svg">
<img src="graphs/7666.svg">
</html>
即使我单独访问这些链接中的每一个都有效,也会失败。我还尝试通过将 file:///Users/my_name/math_project/file.html 作为 url 直接访问 html,并将 {0} 等替换为数字,这很有效。我已经束手无策了,如果有人能帮助我,我将不胜感激。这是用于生成页面的源代码
import socket
import threading
import random
import os
import time
os.chdir("/users/my_name/math_project")
file = open("metadata.txt",'r') # file containing svg file names and equations
svg_data = file.read().split("\n")
file.close()
def handle_call(clientsocket,location): # handles a request
l = time.time()
res = clientsocket.recv(100000)
res = res.split(b"\n")
request = res[0].split(b" ")[1]
if request == b"/favicon.ico":
file = open("favicon.ico",'rb')
stuff2 = file.read()
file.close()
clientsocket.send(stuff2)
if request == b'/':
file = open("file.html",'rb')
res = file.read()
file.close()
res = str(res,'utf-8')
res = res.format(*[random.choice(svg_data).split(":")[0] for a in range(4)])
clientsocket.send(bytes(res,'utf-8'))
if request.startswith(b"/graphs/"):
file = open(place[1:],'rb')
stuff = file.read()
file.close()
clientsocket.send(stuff)
sock = socket.socket()
port = random.randint(2000,10000)
while 1:
try:
sock.bind(('',port))
break
except BaseException:
port = random.randint(2000,10000)
print("Bound to port {0}".format(port))
sock.listen(10)
while 1:
threading.Thread(target=handle_call,args=sock.accept(),).start()
【问题讨论】:
标签: python svg matplotlib