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

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)] on win32

Type "copyright", "credits" or "license()" for more information.

>>> ================================ RESTART ================================

>>>

b'\xc0\x00\x00\x00'

>>> ================================ RESTART ================================

>>>

Traceback (most recent call last):

File "C:\Users\Nevii\Desktop\client.py", line 21, in

tx_buf = struct.pack('

struct.error: char format requires a bytes object of length 1

>>> ================================ RESTART ================================

>>>

Traceback (most recent call last):

File "C:\Users\Nevii\Desktop\client.py", line 21, in

tx_buf = struct.pack('

struct.error: byte format requires -128 <= number <= 127

>>> ================================ RESTART ================================

>>>

Traceback (most recent call last):

File "C:\Users\Nevii\Desktop\client.py", line 21, in

tx_buf = struct.pack('

struct.error: byte format requires -128 <= number <= 127

>>> ================================ RESTART ================================

>>>

Traceback (most recent call last):

File "C:\Users\Nevii\Desktop\client.py", line 21, in

tx_buf = struct.pack('

struct.error: byte format requires -128 <= number <= 127

>>> ================================ RESTART ================================

>>>

Traceback (most recent call last):

File "C:\Users\Nevii\Desktop\client.py", line 21, in

tx_buf = struct.pack('

struct.error: pack expected 2 items for packing (got 1)

>>> ================================ RESTART ================================

>>>

b'\xc0\x00\x00\x00'

>>> ================================ RESTART ================================

>>>

b'\x01\x00\x00\x00\x02\x00\x03'

(1, 2, 3)

b'\x00\x00\x00\x01\x00\x02\x03'

(1, 2, 3)

>>> ================================ RESTART ================================

>>>

Traceback (most recent call last):

File "C:\Users\Nevii\Desktop\client.py", line 21, in

tx_buf = struct.pack('c', cmd_word)

struct.error: char format requires a bytes object of length 1

>>> ================================ RESTART ================================

>>>

Traceback (most recent call last):

File "C:\Users\Nevii\Desktop\client.py", line 21, in

print(len(cmd_word))

TypeError: object of type 'int' has no len()

>>> ================================ RESTART ================================

>>>

4

Traceback (most recent call last):

File "C:\Users\Nevii\Desktop\client.py", line 22, in

tx_buf = struct.pack('c', cmd_word)

struct.error: char format requires a bytes object of length 1

>>> ================================ RESTART ================================

>>>

Traceback (most recent call last):

File "C:\Users\Nevii\Desktop\client.py", line 21, in

type(comd_word)

NameError: name 'comd_word' is not defined

>>> ================================ RESTART ================================

>>>

Traceback (most recent call last):

File "C:\Users\Nevii\Desktop\client.py", line 22, in

tx_buf = struct.pack('c', cmd_word)

struct.error: char format requires a bytes object of length 1

>>> ================================ RESTART ================================

>>>

>>> ================================ RESTART ================================

>>>

>>> ================================ RESTART ================================

>>>

Traceback (most recent call last):

File "C:\Users\Nevii\Desktop\client.py", line 22, in

tx_buf = struct.pack('>=c', cmd_word)

struct.error: bad char in struct format

>>> ================================ RESTART ================================

>>>

Traceback (most recent call last):

File "C:\Users\Nevii\Desktop\client.py", line 22, in

tx_buf = struct.pack('=>c', cmd_word)

struct.error: bad char in struct format

>>> ================================ RESTART ================================

>>>

Traceback (most recent call last):

File "C:\Users\Nevii\Desktop\client.py", line 22, in

tx_buf = struct.pack('=>i', cmd_word)

struct.error: bad char in struct format

>>> ================================ RESTART ================================

>>>

b'\x00\x00\x00\xc0'

>>> ================================ RESTART ================================

>>>

b'\x00\x00\x00\xc0'

Traceback (most recent call last):

File "C:\Users\Nevii\Desktop\client.py", line 24, in

s.send(0xC0)

TypeError: 'int' does not support the buffer interface

>>> ================================ RESTART ================================

>>>

b'\x00\x00\x00\xc0'

>>> ================================ RESTART ================================

>>>

Traceback (most recent call last):

File "C:\Users\Nevii\Desktop\client.py", line 22, in

tx_buf = struct.pack('>c', str(cmd_word))

struct.error: char format requires a bytes object of length 1

>>> ================================ RESTART ================================

>>>

Traceback (most recent call last):

File "C:\Users\Nevii\Desktop\client.py", line 22, in

tx_buf = struct.pack('>b', cmd_word)

struct.error: byte format requires -128 <= number <= 127

>>> ================================ RESTART ================================

>>>

Traceback (most recent call last):

File "C:\Users\Nevii\Desktop\client.py", line 22, in

tx_buf = struct.pack('>b', cmd_word)

struct.error: byte format requires -128 <= number <= 127

>>> ================================ RESTART ================================


版权声明:本文为weixin_39727402原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。