|
|

楼主 |
发表于 2015-9-24 20:26:03
|
显示全部楼层
本帖最后由 SmileN 于 2015-9-24 20:28 编辑
被我一个个的试出来了
- import socket
- import sys
- import struct
- import ctypes
- host = '128.64.6.103'
- port = 37000
- address = (host, port)
- s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
- sa = (host, port)
- s.connect(sa)
- if s is None:
- print ('could not open socket! ')
- sys.exit(1)
- cmd_word = int(0xC0)
- print(type(cmd_word))
- tx_buf = struct.pack('>B', cmd_word)
- print(repr(tx_buf))
- s.send(tx_buf)
复制代码
打印的结果
- >>>
- <class 'int'>
- b'\xc0'
- >>>
复制代码
痛苦的过程
- 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 <module>
- 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 <module>
- 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 21, in <module>
- tx_buf = struct.pack('<b', 0xc0)
- struct.error: byte format requires -128 <= number <= 127
- >>> ================================ RESTART ================================
- >>>
- Traceback (most recent call last):
- File "C:\Users\Nevii\Desktop\client.py", line 21, in <module>
- tx_buf = struct.pack('<b', 0xc0)
- struct.error: byte format requires -128 <= number <= 127
- >>> ================================ RESTART ================================
- >>>
- Traceback (most recent call last):
- File "C:\Users\Nevii\Desktop\client.py", line 21, in <module>
- tx_buf = struct.pack('<ii', 0xc0)
- 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 <module>
- 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 <module>
- 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 <module>
- 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 <module>
- 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 <module>
- tx_buf = struct.pack('c', cmd_word)
- struct.error: char format requires a bytes object of length 1
- >>> ================================ RESTART ================================
- >>>
- >>> ================================ RESTART ================================
- >>>
- <class 'int'>
- >>> ================================ RESTART ================================
- >>>
- <class 'int'>
- Traceback (most recent call last):
- File "C:\Users\Nevii\Desktop\client.py", line 22, in <module>
- tx_buf = struct.pack('>=c', cmd_word)
- struct.error: bad char in struct format
- >>> ================================ RESTART ================================
- >>>
- <class 'int'>
- Traceback (most recent call last):
- File "C:\Users\Nevii\Desktop\client.py", line 22, in <module>
- tx_buf = struct.pack('=>c', cmd_word)
- struct.error: bad char in struct format
- >>> ================================ RESTART ================================
- >>>
- <class 'int'>
- Traceback (most recent call last):
- File "C:\Users\Nevii\Desktop\client.py", line 22, in <module>
- tx_buf = struct.pack('=>i', cmd_word)
- struct.error: bad char in struct format
- >>> ================================ RESTART ================================
- >>>
- <class 'int'>
- b'\x00\x00\x00\xc0'
- >>> ================================ RESTART ================================
- >>>
- <class 'int'>
- b'\x00\x00\x00\xc0'
- Traceback (most recent call last):
- File "C:\Users\Nevii\Desktop\client.py", line 24, in <module>
- s.send(0xC0)
- TypeError: 'int' does not support the buffer interface
- >>> ================================ RESTART ================================
- >>>
- <class 'int'>
- b'\x00\x00\x00\xc0'
- >>> ================================ RESTART ================================
- >>>
- <class 'int'>
- Traceback (most recent call last):
- File "C:\Users\Nevii\Desktop\client.py", line 22, in <module>
- tx_buf = struct.pack('>c', str(cmd_word))
- struct.error: char format requires a bytes object of length 1
- >>> ================================ RESTART ================================
- >>>
- <class 'int'>
- Traceback (most recent call last):
- File "C:\Users\Nevii\Desktop\client.py", line 22, in <module>
- tx_buf = struct.pack('>b', cmd_word)
- struct.error: byte format requires -128 <= number <= 127
- >>> ================================ RESTART ================================
- >>>
- <class 'int'>
- Traceback (most recent call last):
- File "C:\Users\Nevii\Desktop\client.py", line 22, in <module>
- tx_buf = struct.pack('>b', cmd_word)
- struct.error: byte format requires -128 <= number <= 127
- >>> ================================ RESTART ================================
复制代码 |
评分
-
查看全部评分
|