鱼C论坛

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

如何在Python3 TCP/IP 通信中 只发送一个字节

[复制链接]
发表于 2015-9-24 20:04:09 | 显示全部楼层 |阅读模式

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

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

x
大家好,请教大家一个问题,如何在Python3的TCP/IP连接中,只发送一个字节
我的代码如下:
  1. import socket
  2. import sys
  3. import struct
  4. import ctypes


  5. host = '128.64.6.103'
  6. port = 37000

  7. address = (host, port)  
  8. s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  9. sa = (host, port)
  10. s.connect(sa)

  11. if s is None:
  12.     print ('could not open socket! ')
  13.     sys.exit(1)


  14. cmd_word = 0xC0
  15. tx_buf = struct.pack('<I', cmd_word)
  16. print(repr(tx_buf))
  17. s.send(tx_buf)
复制代码


但是每次都发送了四个字节
打印出来的时候

  1. >>>
  2. b'\xc0\x00\x00\x00'
复制代码


我尝试过 I 改成 c 或者 x 均报错,请问如何才能发送定长的1个字节 ,谢谢!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2015-9-24 20:26:03 | 显示全部楼层
本帖最后由 SmileN 于 2015-9-24 20:28 编辑

被我一个个的试出来了

  1. import socket
  2. import sys
  3. import struct
  4. import ctypes


  5. host = '128.64.6.103'
  6. port = 37000

  7. address = (host, port)  
  8. s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  9. sa = (host, port)
  10. s.connect(sa)

  11. if s is None:
  12.     print ('could not open socket! ')
  13.     sys.exit(1)


  14. cmd_word = int(0xC0)
  15. print(type(cmd_word))
  16. tx_buf = struct.pack('>B', cmd_word)
  17. print(repr(tx_buf))
  18. s.send(tx_buf)
复制代码



打印的结果
  1. >>>
  2. <class 'int'>
  3. b'\xc0'
  4. >>>
复制代码



痛苦的过程
  1. Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)] on win32
  2. Type "copyright", "credits" or "license()" for more information.
  3. >>> ================================ RESTART ================================
  4. >>>
  5. b'\xc0\x00\x00\x00'
  6. >>> ================================ RESTART ================================
  7. >>>
  8. Traceback (most recent call last):
  9.   File "C:\Users\Nevii\Desktop\client.py", line 21, in <module>
  10.     tx_buf = struct.pack('<c', cmd_word)
  11. struct.error: char format requires a bytes object of length 1
  12. >>> ================================ RESTART ================================
  13. >>>
  14. Traceback (most recent call last):
  15.   File "C:\Users\Nevii\Desktop\client.py", line 21, in <module>
  16.     tx_buf = struct.pack('<b', cmd_word)
  17. struct.error: byte format requires -128 <= number <= 127
  18. >>> ================================ RESTART ================================
  19. >>>
  20. Traceback (most recent call last):
  21.   File "C:\Users\Nevii\Desktop\client.py", line 21, in <module>
  22.     tx_buf = struct.pack('<b', 0xc0)
  23. struct.error: byte format requires -128 <= number <= 127
  24. >>> ================================ RESTART ================================
  25. >>>
  26. Traceback (most recent call last):
  27.   File "C:\Users\Nevii\Desktop\client.py", line 21, in <module>
  28.     tx_buf = struct.pack('<b', 0xc0)
  29. struct.error: byte format requires -128 <= number <= 127
  30. >>> ================================ RESTART ================================
  31. >>>
  32. Traceback (most recent call last):
  33.   File "C:\Users\Nevii\Desktop\client.py", line 21, in <module>
  34.     tx_buf = struct.pack('<ii', 0xc0)
  35. struct.error: pack expected 2 items for packing (got 1)
  36. >>> ================================ RESTART ================================
  37. >>>
  38. b'\xc0\x00\x00\x00'
  39. >>> ================================ RESTART ================================
  40. >>>
  41. b'\x01\x00\x00\x00\x02\x00\x03'
  42. (1, 2, 3)
  43. b'\x00\x00\x00\x01\x00\x02\x03'
  44. (1, 2, 3)
  45. >>> ================================ RESTART ================================
  46. >>>
  47. Traceback (most recent call last):
  48.   File "C:\Users\Nevii\Desktop\client.py", line 21, in <module>
  49.     tx_buf = struct.pack('c', cmd_word)
  50. struct.error: char format requires a bytes object of length 1
  51. >>> ================================ RESTART ================================
  52. >>>
  53. Traceback (most recent call last):
  54.   File "C:\Users\Nevii\Desktop\client.py", line 21, in <module>
  55.     print(len(cmd_word))
  56. TypeError: object of type 'int' has no len()
  57. >>> ================================ RESTART ================================
  58. >>>
  59. 4
  60. Traceback (most recent call last):
  61.   File "C:\Users\Nevii\Desktop\client.py", line 22, in <module>
  62.     tx_buf = struct.pack('c', cmd_word)
  63. struct.error: char format requires a bytes object of length 1
  64. >>> ================================ RESTART ================================
  65. >>>
  66. Traceback (most recent call last):
  67.   File "C:\Users\Nevii\Desktop\client.py", line 21, in <module>
  68.     type(comd_word)
  69. NameError: name 'comd_word' is not defined
  70. >>> ================================ RESTART ================================
  71. >>>
  72. Traceback (most recent call last):
  73.   File "C:\Users\Nevii\Desktop\client.py", line 22, in <module>
  74.     tx_buf = struct.pack('c', cmd_word)
  75. struct.error: char format requires a bytes object of length 1
  76. >>> ================================ RESTART ================================
  77. >>>
  78. >>> ================================ RESTART ================================
  79. >>>
  80. <class 'int'>
  81. >>> ================================ RESTART ================================
  82. >>>
  83. <class 'int'>
  84. Traceback (most recent call last):
  85.   File "C:\Users\Nevii\Desktop\client.py", line 22, in <module>
  86.     tx_buf = struct.pack('>=c', cmd_word)
  87. struct.error: bad char in struct format
  88. >>> ================================ RESTART ================================
  89. >>>
  90. <class 'int'>
  91. Traceback (most recent call last):
  92.   File "C:\Users\Nevii\Desktop\client.py", line 22, in <module>
  93.     tx_buf = struct.pack('=>c', cmd_word)
  94. struct.error: bad char in struct format
  95. >>> ================================ RESTART ================================
  96. >>>
  97. <class 'int'>
  98. Traceback (most recent call last):
  99.   File "C:\Users\Nevii\Desktop\client.py", line 22, in <module>
  100.     tx_buf = struct.pack('=>i', cmd_word)
  101. struct.error: bad char in struct format
  102. >>> ================================ RESTART ================================
  103. >>>
  104. <class 'int'>
  105. b'\x00\x00\x00\xc0'
  106. >>> ================================ RESTART ================================
  107. >>>
  108. <class 'int'>
  109. b'\x00\x00\x00\xc0'
  110. Traceback (most recent call last):
  111.   File "C:\Users\Nevii\Desktop\client.py", line 24, in <module>
  112.     s.send(0xC0)
  113. TypeError: 'int' does not support the buffer interface
  114. >>> ================================ RESTART ================================
  115. >>>
  116. <class 'int'>
  117. b'\x00\x00\x00\xc0'
  118. >>> ================================ RESTART ================================
  119. >>>
  120. <class 'int'>
  121. Traceback (most recent call last):
  122.   File "C:\Users\Nevii\Desktop\client.py", line 22, in <module>
  123.     tx_buf = struct.pack('>c', str(cmd_word))
  124. struct.error: char format requires a bytes object of length 1
  125. >>> ================================ RESTART ================================
  126. >>>
  127. <class 'int'>
  128. Traceback (most recent call last):
  129.   File "C:\Users\Nevii\Desktop\client.py", line 22, in <module>
  130.     tx_buf = struct.pack('>b', cmd_word)
  131. struct.error: byte format requires -128 <= number <= 127
  132. >>> ================================ RESTART ================================
  133. >>>
  134. <class 'int'>
  135. Traceback (most recent call last):
  136.   File "C:\Users\Nevii\Desktop\client.py", line 22, in <module>
  137.     tx_buf = struct.pack('>b', cmd_word)
  138. struct.error: byte format requires -128 <= number <= 127
  139. >>> ================================ RESTART ================================
复制代码

评分

参与人数 1荣誉 +3 鱼币 +5 收起 理由
~风介~ + 3 + 5 不错嘛,自己解决了~

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-9-25 20:11:51 | 显示全部楼层
楼主厉害!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-9-30 13:36:33 | 显示全部楼层
怎么不看文档呢?文档说的很详细,有个表格。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-10-12 10:08:35 | 显示全部楼层
我是桃川人 发表于 2015-9-30 13:36
怎么不看文档呢?文档说的很详细,有个表格。

:cry 其实是我悟性差,好久才看懂....
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-17 17:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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