python - 向客户端发送字符串 - 接收 TypeError
问题描述
我正在尝试向客户端发送图像,因此我已将图像转换为字符串。这是我的代码:
def send_image_to_client(file_name, client_socket):
"""sends an image to the client"""
with open(file_name, "rb") as im:
string = base64.b64encode(im.read()) # convert image into a string
print "sending image..."
client_socket.send(string)
return
出于某种原因,我收到以下错误:
send() 参数 1 必须是字符串或缓冲区,而不是 None
我究竟做错了什么?
此示例显示相同的错误:
def show_content(directory, client_socket):
"""displays the content of a given directory"""
import glob
files_list = glob.glob(directory+"\*.*")
files_list[-1] += "--"
for address in files_list:
if address:
send_response_to_client(address+"*", client_socket)
return
解决方案
上面的代码确实是您正在尝试的代码,或者您在将其发布到此处之前已经进行了任何清理?你可以试试这个——
with open(file_name, "rb") as im:
string = base64.b64encode(im.read()) # convert image into a string
print "sending image..."
print(type(string))
if (string):
client_socket.send(string.encode())
else:
print("No Data received from base64encode")
return
推荐阅读
- angular - 如何使用 Angular 测试窗口事件的滚动?
- python - 为什么在 PyCharm 中安装 AutoPy 时会出错
- sql - 在 Postgres 中根据减去的数据创建平均值
- vba - Vba 在一个过程中有两个不同的错误处理程序
- c# - 是否可以使用值复制语义实现 ac# 类?
- python - 关于在 Python 中进行并行触发和忘记函数调用的任何建议?
- javascript - 如何链接 Intl.Collator().compare?
- itemtemplate - RadListView OnSelectedIndexChanged ...如何获取所选项目的选定索引
- html - 引导背景图像出现在我网站的每个页面上
- angular - 如何删除primeng条形图条上的数字标签?