import socket
HOST = '127.0.0.1' # The remote host
PORT = 50010 # The same port as used by the server
count=0
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.sendall(b'Hello, world')
while count<10:
count+=1
data = s.recv(1024)
print(data)
print(str(type(data)))
print(str(type(count)))
#data=data+data
data=data+bytes(count)
s.sendall(data)
print('Received', repr(data))
运行结果:
b'Hello, world'
<class 'bytes'>
<class 'int'>
b'Hello, world\x00'
<class 'bytes'>
<class 'int'>
b'Hello, world\x00\x00\x00'
<class 'bytes'>
<class 'int'>
b'Hello, world\x00\x00\x00\x00\x00\x00'
<class 'bytes'>
<class 'int'>
b'Hello, world\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
<class 'bytes'>
<class 'int'>
b'Hello, world\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
<class 'bytes'>
<class 'int'>
b'Hello, world\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
<class 'bytes'>
<class 'int'>
b'Hello, world\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
<class 'bytes'>
<class 'int'>
b'Hello, world\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
<class 'bytes'>
<class 'int'>
b'Hello, world\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
<class 'bytes'>
<class 'int'>
Received b'Hello, world\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
>>>
如何把整型count转换为bytes类型呢?