import socket,os
server=socket.socket()
server.bind(('localhost',9999))
server.listen()
while True:
conn,addr=server.accept()
print("new conn:",addr)
while True:
data=conn.recv(1024)
if not data:
print("客户端已断,请重新输入")
break
cmd_res=os.popen(data.decode('utf-8')).read()
if len(cmd_res)==0:
cmd_res='无效命令,没有返回值'
conn.send(cmd_res.encode('utf-8')) server.close()
import socket client=socket.socket()
client.connect(('localhost',9999))
while True:
cmd=input(">>:").strip()
if len(cmd)==0:
continue
client.send(cmd.encode('utf-8'))
cmd_res=client.recv(1024)
print(cmd_res.decode('utf-8'))
client.close()
服务器端的输出
new conn: ('127.0.0.1', 61966)
'pwd' is not recognized as an internal or external command,
operable program or batch file.
客户端的输出
>>:pwd
无效命令,没有返回值
>>:dir
Volume in drive C is OSDisk
Volume Serial Number is 002E-D604 Directory of C:\abccdxddd\Oldboy\Py_Exercise\Day8 08/07/2017 03:54 PM <DIR> .
08/07/2017 03:54 PM <DIR> ..
08/07/2017 03:52 PM 280 client_ch.py
08/07/2017 03:54 PM 509 server_ch.py
08/03/2017 11:17 AM 650 socket_ssh_server.py
08/03/2017 11:26 AM 259 sock_ssh_client.py
08/03/2017 03:56 PM 133 ssh_client.py
08/03/2017 03:54 PM 415 ssh_server.py
08/03/2017 04:55 PM 259 ????.py
08/07/2017 10:27 AM 146 ????.py
8 File(s) 2,651 bytes
2 Dir(s) 145,735,159,808 bytes free
让超过1024大小的数据的一次返回,先在服务器端计算大小,把需要接受的次数传给客户端。
服务器端:
import socket,os server=socket.socket()
server.bind(('localhost',9999))
server.listen()
while True:
conn,addr=server.accept()
print("new connection is :",addr)
while True:
data=conn.recv(1024)
if not data:
print("客户端已经断开")
break
cmd_res=os.popen(data.decode('utf-8')).read()
if len(cmd_res)==0:
cmd_res="命令无效,没有返回值"
conn.send(str(len(cmd_res)).encode('utf-8'))
#整数不能直接encode,需要先转成字符串,再encode.发大小给客户端
conn.send(cmd_res.encode('utf-8'))
server.close()
客户端:
import socket client=socket.socket()
client.connect(('localhost',9999))
while True:
cmd=input(">>:").strip()
if len(cmd)==0:
break
client.send(cmd.encode('utf-8'))
cmd_res_size=client.recv(1024) #接收命令结果的长度。
print("命令结果大小:",cmd_res_size)
received_size=0
received_data=b""
while received_size<int(cmd_res_size.decode("utf-8")):
data=client.recv(1024)
received_size=received_size+len(data) #每次收到的有效数字,有可能小于1024
received_data=received_data+data
else:
print("cmd res received done...",received_size)
print(received_data.decode('utf-8'))
client.close()
实现收到的数据大小跟要传输的数据大小相一致。
import socket,os server=socket.socket()
server.bind(('localhost',9999))
server.listen()
while True:
conn,addr=server.accept()
print("new connection is :",addr)
while True:
data=conn.recv(1024)
if not data:
print("客户端已经断开")
break
cmd_res=os.popen(data.decode('utf-8')).read()
if len(cmd_res)==0:
cmd_res="命令无效,没有返回值"
conn.send(str(len(cmd_res.encode())).encode('utf-8'))
#整数不能直接encode,需要先转成字符串,再encode.发大小给客户端
conn.send(cmd_res.encode('utf-8'))
server.close()
客户端:
import socket client=socket.socket()
client.connect(('localhost',9999))
while True:
cmd=input(">>:").strip()
if len(cmd)==0:
break
client.send(cmd.encode('utf-8'))
cmd_res_size=client.recv(1024) #接收命令结果的长度。
print("命令结果大小:",cmd_res_size)
received_size=0
received_data=b""
while received_size<int(cmd_res_size.decode("utf-8")):
data=client.recv(1024)
received_size=received_size+len(data) #每次收到的有效数字,有可能小于1024
received_data=received_data+data
else:
print("cmd res received done...",received_size)
print(received_data.decode('utf-8'))
client.close()
关于字符长度:
aa="人"
print(len(aa))
print(len(aa.encode('utf-8')))
结果输出:
1
3