くだすれPython(超初心者用)
at TECH
381:デフォルトの名無しさん
08/05/23 15:16:59
>>373-374
使用環境をまったく書いてないのは質問として NG だと思う。
サーバがWindows環境だとバイナリファイルの扱いに注意が必要だから。
CGI 作成の注意としては、ヘッダの行末は \n ではなく \r\n でないといけないはず。
> print f.read(),
ここも1バイト余計にデータを送っていることになるのでよくない。
手元の Windows マシンでは Python 付属の CGIHTTPServer を使って以下のコードで動いた。
import sys, os
import cgi
import cgitb; cgitb.enable()
if sys.platform == "win32":
import msvcrt
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
filepath = "test.jpg"
filename = os.path.split(filepath)
filename = filename[1]
filesize = os.path.getsize(filepath)
sys.stdout.write('Content-Type: application/octet-stream\r\n')
sys.stdout.write('Content-Disposition: attachment; filename="%s"\r\n' % filename)
sys.stdout.write('Content-Length: %d\r\n' % filesize)
sys.stdout.write('\r\n')
f = open(filepath, "rb")
sys.stdout.write(f.read())
sys.stdout.flush()
f.close()
次ページ続きを表示1を表示最新レス表示スレッドの検索類似スレ一覧話題のニュースおまかせリスト▼オプションを表示暇つぶし2ch
5286日前に更新/337 KB
担当:undef