使用Python CGI发送.png文件

1 投票
1 回答
2213 浏览
提问于 2025-04-15 23:30

我该如何用Python的CGI把一个.png文件发送到一个Flex应用程序呢?

提前谢谢你...

1 个回答

3

你提到的Python/CGI部分其实可以很简单,比如说如果你只是想发送一张已经存在的图片,可以这样做:

import sys

# Send the Content-Type header to let the client know what you're sending
sys.stdout.write('Content-Type: image/png\r\n\r\n')

# Send the actual image data
with open('path/to/image.png', 'rb') as f:
    sys.stdout.write(f.read())

另一方面,如果你是用比如说PIL动态生成图片,那你可以这样做:

import sys
import Image, ImageDraw # or whatever

sys.stdout.write('Content-Type: image/png\r\n\r\n')

# Dynamically create an image
image = Image.new('RGB', (100, 100))
# ... etc ...

# Send the image to the client
image.save(sys.stdout, 'PNG')

撰写回答