![](/img/trans.png)
[英]Why can't I use requests library to access a website while I can still visit it from the browser?
[英]I can open picture from local, but when I visit from website there is always present contain some errors
我可以在本地訪問我的圖片,但是當我希望從django服務器進行訪問時,總是會出現錯誤。
源代碼:
from django.http.response import HttpResponse
import mimetypes
fd = open(CONFIG.SERVICES_PATH + sname+'/'+url,'r')
print CONFIG.SERVICES_PATH + sname+'/'+url
mime_type_guess = mimetypes.guess_type(url)
print mime_type_guess
data = fd.read()
fd.close()
response = HttpResponse(data,mimetype = mime_type_guess[0])
在控制台上打印出來的是:
E:/workspace/sydney/main/services/Hunt-Club/shop/1.jpg
('image/pjpeg', None)
我可以從本地路徑訪問圖片,但是當我運行django服務器並從瀏覽器訪問它時,出現了以下錯誤:
無法顯示http:// localhost:8000 / gallery / image / Hunt-Club / shop / 1 /”,因為其中包含錯誤。“>
我不知道是否要給出正確的路徑並讀取數據,圖片要求仍然存在錯誤。
您可能需要重新考慮通過django應用程序提供文件。 相反,您應該從/ static文件夾中為它們提供服務,該文件夾在apache設置中將使用別名進行配置。
話雖如此,請查看以下內容: image / pjpeg和image / jpeg
它說可能與向IE提供pjpeg內容類型有關。
希望能有所幫助。
我發現我的錯誤在哪里,讀取的圖像文件代碼應為:
fd = open(CONFIG.SERVICES_PATH + sname+'/'+url,'rb')
因為在Windows中,打開文件默認應用以ASCII打開,所以文件無法正確讀取。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.