鱼C论坛

 找回密码
 立即注册
查看: 741|回复: 4

[已解决]socket库上传文件错误。

[复制链接]
发表于 2020-4-6 16:04:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 xdyewu 于 2020-4-6 16:24 编辑

第一种:
  1. def send_file(serve_addr,tcp_socket):
  2.     send_file_name = input("请输入要上传的文件文件名:")
  3.     file_content = None
  4.     file = open(send_file_name,"rb")
  5.     file_content = file.read
  6.     file.close()
  7.     if file_content:
  8.         tcp_socket.send(send_file_name.encode("utf-8"))
  9.         tcp_socket.send(file_content)
复制代码


第二种:
  1. def send_file(serve_addr,tcp_socket):
  2.     send_file_name = input("请输入要上传的文件文件名:")
  3.     file_content = None
  4.     file_content = file_del(send_file_name)
  5.     if file_content:
  6.         tcp_socket.send(send_file_name.encode("utf-8"))
  7.         tcp_socket.send(file_content)
  8.         print("文件上传成功")
  9.     else :
  10.         print("文件上传失败")



  11. def file_del(send_file_name):
  12.     try:
  13.         date = open(send_file_name,"rb")
  14.         file_date = date.read()
  15.         date.close()
  16.         return file_date
  17.     except Exception:
  18.         print("文件异常")
复制代码



采用第二种方式出来的代码可以正常使用,而我用第一种方法出来的代码出现了这种错误提示。
“tcp_socket.send(file_content)”
”TypeError: a bytes-like object is required, not 'builtin_function_or_method'“

希望各位大佬可以跟我解释一下这种情况,我后面加.encode()也没有办法运行。
最佳答案
2020-4-6 16:34:09
xdyewu 发表于 2020-4-6 16:28
不好意思,原来传的代码有点问题,第一种方法原来没有 file_deal方法。重新修改了。
file_count打印后是 ...

一楼第一种第⑤行read后面是否有()
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-6 16:09:08 | 显示全部楼层
你打印一下那个file_content,看看他是什么
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-6 16:28:04 | 显示全部楼层
qiuyouzhi 发表于 2020-4-6 16:09
你打印一下那个file_content,看看他是什么

不好意思,原来传的代码有点问题,第一种方法原来没有 file_deal方法。重新修改了。
file_count打印后是这个数据<built-in method read of _io.BufferedReader object at 0x00000158DF0CF0F0>
那个文件里面存的是hallo
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-6 16:34:09 From FishC Mobile | 显示全部楼层    本楼为最佳答案   
xdyewu 发表于 2020-4-6 16:28
不好意思,原来传的代码有点问题,第一种方法原来没有 file_deal方法。重新修改了。
file_count打印后是 ...

一楼第一种第⑤行read后面是否有()
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-6 17:18:06 | 显示全部楼层
wp231957 发表于 2020-4-6 16:34
一楼第一种第⑤行read后面是否有()

对了 。就这错了 。感谢感谢,小白一名,没注意到这里。太尴尬了 。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-22 00:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表