Python2
- 方法一:
print(data)
- 方法二:
sys.stdout.write(data)
Python3
- 方法一:
sys.stdout.buffer.write(data)
该方法可能在某些http服务中失效,因为没有sys.stdout.buffer
- 方法二:打开标准输出文件
with os.fdopen(sys.stdout.fileno(), 'wb', closefd=False) as stdout:
stdout.write(data)
stdout.flush()
'''
解析:
sys.stdout.fileno():标准输出文件描述符,通常是1
closefd=False:with代码块结束后不关闭标准输出文件,避免后续无法使用标准输出
stdout.flush():强制写入数据,避免数据在缓存区中堆积
'''