创建cmd_server.py和cmd_client.py文件

subprocess模块

#__author__ = "Smoke"
#date = 2020/11/22 22:03


import subprocess

subprocess.Popen('dir',shell=True)

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/test.py
3menu_latest.py		function_\ parameter.py        PICKLE_text
3menu_new.py		function.py		       polymorphic.py
3menu.py		function_return.py	       __pycache__
对齐.py			function_scope.py	       random_module.py
格式化输出.py		generator.py		       reflex.py
日志记录		hashlib_module.py	       re_module.py
深浅拷贝.py		hello			       s1.py
小重山			i.cfg			       s2.py
小重山2			inherit.py		       serve.py
assignment.py		__init__.py		       set.py
built-in-funcation.py	iterator.py		       SHELVE_test
calculator.py		jingdong_passwd		       SHELVE_test.py
cal.py			JSON_loads.py		       shopping_car_login.py
client			json_test.py		       shopping_cart.py
client.py		JSON_text		       shopping.py
closure.py		JSON_text2		       Singleton_mode.py
cmd_client.py		list_builder.py		       smoke
cmd_server.py		list_lesson.py		       special_members.py
code\ template.py	log			       String.py
configparser_module.py	logging_module.py	       sys_module.py
continue.py		login.py		       test
decrator.py		login_while.py		       test.log
dictionary.py		member_qualifier_character.py  test.py
encapsulation.py	menu.py			       test.xml
encode_py3.py		module_project		       three_level_menu
example.ini		__name__.py		       time_module.py
exception_handling.py	object_oriented.py	       tuple.py
field.py		os_module.py		       weixin_passwd
file_operation2.py	output.xml		       xml_test
file_operation.py	PICKLE_load.py		       XML_test.py
funcation_recursion.py	PICKLE_test.py		       xmltest.xml

Process finished with exit code 0

#__author__ = "Smoke"
#date = 2020/11/22 22:03


import subprocess

a = subprocess.Popen('dir',shell=True)

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/test.py
3menu_latest.py		function_\ parameter.py        PICKLE_text
3menu_new.py		function.py		       polymorphic.py
3menu.py		function_return.py	       __pycache__
对齐.py			function_scope.py	       random_module.py
格式化输出.py		generator.py		       reflex.py
日志记录		hashlib_module.py	       re_module.py
深浅拷贝.py		hello			       s1.py
小重山			i.cfg			       s2.py
小重山2			inherit.py		       serve.py
assignment.py		__init__.py		       set.py
built-in-funcation.py	iterator.py		       SHELVE_test
calculator.py		jingdong_passwd		       SHELVE_test.py
cal.py			JSON_loads.py		       shopping_car_login.py
client			json_test.py		       shopping_cart.py
client.py		JSON_text		       shopping.py
closure.py		JSON_text2		       Singleton_mode.py
cmd_client.py		list_builder.py		       smoke
cmd_server.py		list_lesson.py		       special_members.py
code\ template.py	log			       String.py
configparser_module.py	logging_module.py	       sys_module.py
continue.py		login.py		       test
decrator.py		login_while.py		       test.log
dictionary.py		member_qualifier_character.py  test.py
encapsulation.py	menu.py			       test.xml
encode_py3.py		module_project		       three_level_menu
example.ini		__name__.py		       time_module.py
exception_handling.py	object_oriented.py	       tuple.py
field.py		os_module.py		       weixin_passwd
file_operation2.py	output.xml		       xml_test
file_operation.py	PICKLE_load.py		       XML_test.py
funcation_recursion.py	PICKLE_test.py		       xmltest.xml

Process finished with exit code 0

#__author__ = "Smoke"
#date = 2020/11/22 22:03


import subprocess

a = subprocess.Popen('dir',shell=True)   #多进程
print(a)

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/test.py
<subprocess.Popen object at 0x7f0d515d2eb0>
3menu_latest.py		function_\ parameter.py        PICKLE_text
3menu_new.py		function.py		       polymorphic.py
3menu.py		function_return.py	       __pycache__
对齐.py			function_scope.py	       random_module.py
格式化输出.py		generator.py		       reflex.py
日志记录		hashlib_module.py	       re_module.py
深浅拷贝.py		hello			       s1.py
小重山			i.cfg			       s2.py
小重山2			inherit.py		       serve.py
assignment.py		__init__.py		       set.py
built-in-funcation.py	iterator.py		       SHELVE_test
calculator.py		jingdong_passwd		       SHELVE_test.py
cal.py			JSON_loads.py		       shopping_car_login.py
client			json_test.py		       shopping_cart.py
client.py		JSON_text		       shopping.py
closure.py		JSON_text2		       Singleton_mode.py
cmd_client.py		list_builder.py		       smoke
cmd_server.py		list_lesson.py		       special_members.py
code\ template.py	log			       String.py
configparser_module.py	logging_module.py	       sys_module.py
continue.py		login.py		       test
decrator.py		login_while.py		       test.log
dictionary.py		member_qualifier_character.py  test.py
encapsulation.py	menu.py			       test.xml
encode_py3.py		module_project		       three_level_menu
example.ini		__name__.py		       time_module.py
exception_handling.py	object_oriented.py	       tuple.py
field.py		os_module.py		       weixin_passwd
file_operation2.py	output.xml		       xml_test
file_operation.py	PICKLE_load.py		       XML_test.py
funcation_recursion.py	PICKLE_test.py		       xmltest.xml

Process finished with exit code 0

#__author__ = "Smoke"
#date = 2020/11/22 22:03


import subprocess

a = subprocess.Popen('dir',shell=True,stdout=subprocess.PIPE)    #subprocess.PIPE通过管道把子进程内容送到主进程
print(a)

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/test.py
<subprocess.Popen object at 0x7f2772a9ceb0>

Process finished with exit code 0

#__author__ = "Smoke"
#date = 2020/11/22 22:03


import subprocess

a = subprocess.Popen('dir',shell=True)
# print(a)
print(a.stdout.read())

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/test.py
3menu_latest.py		function_\ parameter.py        PICKLE_text
3menu_new.py		function.py		       polymorphic.py
3menu.py		function_return.py	       __pycache__
对齐.py			function_scope.py	       random_module.py
格式化输出.py		generator.py		       reflex.py
日志记录		hashlib_module.py	       re_module.py
深浅拷贝.py		hello			       s1.py
小重山			i.cfg			       s2.py
小重山2			inherit.py		       serve.py
assignment.py		__init__.py		       set.py
built-in-funcation.py	iterator.py		       SHELVE_test
calculator.py		jingdong_passwd		       SHELVE_test.py
cal.py			JSON_loads.py		       shopping_car_login.py
client			json_test.py		       shopping_cart.py
client.py		JSON_text		       shopping.py
closure.py		JSON_text2		       Singleton_mode.py
cmd_client.py		list_builder.py		       smoke
cmd_server.py		list_lesson.py		       special_members.py
code\ template.py	log			       String.py
configparser_module.py	logging_module.py	       sys_module.py
continue.py		login.py		       test
decrator.py		login_while.py		       test.log
dictionary.py		member_qualifier_character.py  test.py
encapsulation.py	menu.py			       test.xml
encode_py3.py		module_project		       three_level_menu
example.ini		__name__.py		       time_module.py
exception_handling.py	object_oriented.py	       tuple.py
field.py		os_module.py		       weixin_passwd
file_operation2.py	output.xml		       xml_test
file_operation.py	PICKLE_load.py		       XML_test.py
funcation_recursion.py	PICKLE_test.py		       xmltest.xml
Traceback (most recent call last):
  File "/home/smoke/PycharmProjects/pythonProject/lean_python/test.py", line 9, in <module>
    print(a.stdout.read())
AttributeError: 'NoneType' object has no attribute 'read'

Process finished with exit code 1

#__author__ = "Smoke"
#date = 2020/11/22 22:03


import subprocess

a = subprocess.Popen('dir',shell=True)
# print(a)
print(a.stdout)

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/test.py
None
3menu_latest.py		function_\ parameter.py        PICKLE_text
3menu_new.py		function.py		       polymorphic.py
3menu.py		function_return.py	       __pycache__
对齐.py			function_scope.py	       random_module.py
格式化输出.py		generator.py		       reflex.py
日志记录		hashlib_module.py	       re_module.py
深浅拷贝.py		hello			       s1.py
小重山			i.cfg			       s2.py
小重山2			inherit.py		       serve.py
assignment.py		__init__.py		       set.py
built-in-funcation.py	iterator.py		       SHELVE_test
calculator.py		jingdong_passwd		       SHELVE_test.py
cal.py			JSON_loads.py		       shopping_car_login.py
client			json_test.py		       shopping_cart.py
client.py		JSON_text		       shopping.py
closure.py		JSON_text2		       Singleton_mode.py
cmd_client.py		list_builder.py		       smoke
cmd_server.py		list_lesson.py		       special_members.py
code\ template.py	log			       String.py
configparser_module.py	logging_module.py	       sys_module.py
continue.py		login.py		       test
decrator.py		login_while.py		       test.log
dictionary.py		member_qualifier_character.py  test.py
encapsulation.py	menu.py			       test.xml
encode_py3.py		module_project		       three_level_menu
example.ini		__name__.py		       time_module.py
exception_handling.py	object_oriented.py	       tuple.py
field.py		os_module.py		       weixin_passwd
file_operation2.py	output.xml		       xml_test
file_operation.py	PICKLE_load.py		       XML_test.py
funcation_recursion.py	PICKLE_test.py		       xmltest.xml

Process finished with exit code 0

#__author__ = "Smoke"
#date = 2020/11/22 22:03


import subprocess

a = subprocess.Popen('dir',shell=True,stdout=subprocess.PIPE)
# print(a)
print(a.stdout.read())

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/test.py
b'3menu_latest.py\t\tfunction_\\ parameter.py        PICKLE_text\n3menu_new.py\t\tfunction.py\t\t       polymorphic.py\n3menu.py\t\tfunction_return.py\t       __pycache__\n\xe5\xaf\xb9\xe9\xbd\x90.py\t\t\
tfunction_scope.py\t       random_module.py\n\xe6\xa0\xbc\xe5\xbc\x8f\xe5\x8c\x96\xe8\xbe\x93\xe5\x87\xba.py\t\tgenerator.py\t\t       reflex.py\n\xe6\x97\xa5\xe5\xbf\x97\xe8\xae\xb0\xe5\xbd\x95\t\thashlib
_module.py\t       re_module.py\n\xe6\xb7\xb1\xe6\xb5\x85\xe6\x8b\xb7\xe8\xb4\x9d.py\t\thello\t\t\t       s1.py\n\xe5\xb0\x8f\xe9\x87\x8d\xe5\xb1\xb1\t\t\ti.cfg\t\t\t       s2.py\n\xe5\xb0\x8f\xe9\x87\x8d\
xe5\xb1\xb12\t\t\tinherit.py\t\t       serve.py\nassignment.py\t\t__init__.py\t\t       set.py\nbuilt-in-funcation.py\titerator.py\t\t       SHELVE_test\ncalculator.py\t\tjingdong_passwd\t\t       SHELVE_t
est.py\ncal.py\t\t\tJSON_loads.py\t\t       shopping_car_login.py\nclient\t\t\tjson_test.py\t\t       shopping_cart.py\nclient.py\t\tJSON_text\t\t       shopping.py\nclosure.py\t\tJSON_text2\t\t       Sing
leton_mode.py\ncmd_client.py\t\tlist_builder.py\t\t       smoke\ncmd_server.py\t\tlist_lesson.py\t\t       special_members.py\ncode\\ template.py\tlog\t\t\t       String.py\nconfigparser_module.py\tlogging
_module.py\t       sys_module.py\ncontinue.py\t\tlogin.py\t\t       test\ndecrator.py\t\tlogin_while.py\t\t       test.log\ndictionary.py\t\tmember_qualifier_character.py  test.py\nencapsulation.py\tmenu.p
y\t\t\t       test.xml\nencode_py3.py\t\tmodule_project\t\t       three_level_menu\nexample.ini\t\t__name__.py\t\t       time_module.py\nexception_handling.py\tobject_oriented.py\t       tuple.py\nfield.py
\t\tos_module.py\t\t       weixin_passwd\nfile_operation2.py\toutput.xml\t\t       xml_test\nfile_operation.py\tPICKLE_load.py\t\t       XML_test.py\nfuncation_recursion.py\tPICKLE_test.py\t\t       xmltest.xml\n'

Process finished with exit code 0

#__author__ = "Smoke"
#date = 2020/11/22 22:03


import subprocess

a = subprocess.Popen('dir',shell=True,stdout=subprocess.PIPE)
# print(a)
print(str(a.stdout.read(),'utf8'))

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/test.py
3menu_latest.py		function_\ parameter.py        PICKLE_text
3menu_new.py		function.py		       polymorphic.py
3menu.py		function_return.py	       __pycache__
对齐.py			function_scope.py	       random_module.py
格式化输出.py		generator.py		       reflex.py
日志记录		hashlib_module.py	       re_module.py
深浅拷贝.py		hello			       s1.py
小重山			i.cfg			       s2.py
小重山2			inherit.py		       serve.py
assignment.py		__init__.py		       set.py
built-in-funcation.py	iterator.py		       SHELVE_test
calculator.py		jingdong_passwd		       SHELVE_test.py
cal.py			JSON_loads.py		       shopping_car_login.py
client			json_test.py		       shopping_cart.py
client.py		JSON_text		       shopping.py
closure.py		JSON_text2		       Singleton_mode.py
cmd_client.py		list_builder.py		       smoke
cmd_server.py		list_lesson.py		       special_members.py
code\ template.py	log			       String.py
configparser_module.py	logging_module.py	       sys_module.py
continue.py		login.py		       test
decrator.py		login_while.py		       test.log
dictionary.py		member_qualifier_character.py  test.py
encapsulation.py	menu.py			       test.xml
encode_py3.py		module_project		       three_level_menu
example.ini		__name__.py		       time_module.py
exception_handling.py	object_oriented.py	       tuple.py
field.py		os_module.py		       weixin_passwd
file_operation2.py	output.xml		       xml_test
file_operation.py	PICKLE_load.py		       XML_test.py
funcation_recursion.py	PICKLE_test.py		       xmltest.xml


Process finished with exit code 0

cmd_serve.py

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:cmd_server.py
# time:2021/03/15

import subprocess

import socket

# family type
sk = socket.socket()    #两个参数,family=AF_INET代表ipv4,type=SOCK_STREAM代表tcp,proto=0

print(sk)

address=('127.0.0.1',8000)     #ip和端口
sk.bind(address)    #绑定ip和端口
sk.listen(3)    #3代表tcp/udp连接数

print('waiting......')

while True:

    conn, addr = sk.accept()  # 阻塞,accept接收连接内容
    print(addr)
    while True:
        try:
            data = conn.recv(1024)
        except Exception:
            break
        if not data: break
        print('......', str(data, 'utf8'))
        obj = subprocess.Popen(data,shell=True,stdout=subprocess.PIPE)
        cmd_result = obj.stdout.read()
        conn.send(cmd_result)

sk.close()

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/cmd_server.py
<socket.socket fd=3, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('0.0.0.0', 0)>
waiting......

cmd_client.py

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:cmd_client.py
# time:2021/03/15

import socket

sk = socket.socket()
print(sk)

address = ('127.0.0.1',8000)
sk.connect(address)    #连接127.0.0.1:8000端口

while True:
    inp = input('>>>')
    if inp == 'exit':
        break
    sk.send(bytes(inp,'utf8'))

    data = sk.recv(1024)
    print(str(data,'utf8'))

sk.close()    #关闭连接

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/cmd_client.py
<socket.socket fd=3, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('0.0.0.0', 0)>
>>>dir
3menu_latest.py		function_\ parameter.py        PICKLE_text
3menu_new.py		function.py		       polymorphic.py
3menu.py		function_return.py	       __pycache__
对齐.py			function_scope.py	       random_module.py
格式化输出.py		generator.py		       reflex.py
日志记录		hashlib_module.py	       re_module.py
深浅拷贝.py		hello			       s1.py
小重山			i.cfg			       s2.py
小重山2			inherit.py		       serve.py
assignment.py		__init__.py		       set.py
built-in-funcation.py	iterator.py		       SHELVE_test
calculator.py		jingdong_passwd		       SHELVE_test.py
cal.py			JSON_loads.py		       shopping_car_login.py
client			json_test.py		       shopping_cart.py
client.py		JSON_text		       shopping.py
closure.py		JSON_text2		       Singleton_mode.py
cmd_client.py		list_builder.py		       smoke
cmd_server.py		list_lesson.py		       special_members.py
code\ template.py	log			       String.py
configparser_module.py	logging_module.py	       sys_module.py
continue.py		login.py		       test
decrator.py		login
>>>

cmd_serve.py

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:cmd_server.py
# time:2021/03/15

import subprocess

import socket

# family type
sk = socket.socket()    #两个参数,family=AF_INET代表ipv4,type=SOCK_STREAM代表tcp,proto=0

print(sk)

address=('127.0.0.1',8000)     #ip和端口
sk.bind(address)    #绑定ip和端口
sk.listen(3)    #3代表tcp/udp连接数

print('waiting......')

while True:

    conn, addr = sk.accept()  # 阻塞,accept接收连接内容
    print(addr)
    while True:
        try:
            data = conn.recv(1024)
        except Exception:
            break
        if not data: break
        print('......', str(data, 'utf8'))
        obj = subprocess.Popen(str(data,'utf8'),shell=True,stdout=subprocess.PIPE)
        cmd_result = obj.stdout.read()
        conn.send(cmd_result)

sk.close()

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/cmd_server.py
<socket.socket fd=3, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('0.0.0.0', 0)>
waiting......

cmd_client.py

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:cmd_client.py
# time:2021/03/15

import socket

sk = socket.socket()
print(sk)

address = ('127.0.0.1',8000)
sk.connect(address)    #连接127.0.0.1:8000端口

while True:
    inp = input('>>>')
    if inp == 'exit':
        break
    sk.send(bytes(inp,'utf8'))

    data = sk.recv(1024)
    print(str(data,'utf8'))

sk.close()    #关闭连接

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/cmd_client.py
<socket.socket fd=3, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('0.0.0.0', 0)>
>>>dir
3menu_latest.py		function_\ parameter.py        PICKLE_text
3menu_new.py		function.py		       polymorphic.py
3menu.py		function_return.py	       __pycache__
对齐.py			function_scope.py	       random_module.py
格式化输出.py		generator.py		       reflex.py
日志记录		hashlib_module.py	       re_module.py
深浅拷贝.py		hello			       s1.py
小重山			i.cfg			       s2.py
小重山2			inherit.py		       serve.py
assignment.py		__init__.py		       set.py
built-in-funcation.py	iterator.py		       SHELVE_test
calculator.py		jingdong_passwd		       SHELVE_test.py
cal.py			JSON_loads.py		       shopping_car_login.py
client			json_test.py		       shopping_cart.py
client.py		JSON_text		       shopping.py
closure.py		JSON_text2		       Singleton_mode.py
cmd_client.py		list_builder.py		       smoke
cmd_server.py		list_lesson.py		       special_members.py
code\ template.py	log			       String.py
configparser_module.py	logging_module.py	       sys_module.py
continue.py		login.py		       test
decrator.py		login

>>>cd
_while.py		       test.log
dictionary.py		member_qualifier_character.py  test.py
encapsulation.py	menu.py			       test.xml
encode_py3.py		module_project		       three_level_menu
example.ini		__name__.py		       time_module.py
exception_handling.py	object_oriented.py	       tuple.py
field.py		os_module.py		       weixin_passwd
file_operation2.py	output.xml		       xml_test
file_operation.py	PICKLE_load.py		       XML_test.py
funcation_recursion.py	PICKLE_test.py		       xmltest.xml

>>>ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp4s0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN group default qlen 1000
    link/ether 44:8a:5b:f1:bd:c5 brd ff:ff:ff:ff:ff:ff
3: wlp5s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether d8:fc:93:0f:85:93 brd ff:ff:ff:ff:ff:ff
    inet 192.168.254.20/27 brd 192.168.254.31 scope global dynamic noprefixroute wlp5s0
       valid_lft 4527sec preferred_lft 4527sec
    inet6 fe80::65c8:65dd:a44d:f28c/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

>>>cd    #cd命令不能用了,windows还会执行上一个命令

cmd_serve.py

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:cmd_server.py
# time:2021/03/15

import subprocess

import socket

# family type
sk = socket.socket()    #两个参数,family=AF_INET代表ipv4,type=SOCK_STREAM代表tcp,proto=0

print(sk)

address=('127.0.0.1',8000)     #ip和端口
sk.bind(address)    #绑定ip和端口
sk.listen(3)    #3代表tcp/udp连接数

print('waiting......')

while True:

    conn, addr = sk.accept()  # 阻塞,accept接收连接内容
    print(addr)
    while True:
        try:
            data = conn.recv(1024)
        except Exception:
            break
        if not data: break
        print('......', str(data, 'utf8'))
        obj = subprocess.Popen(str(data,'utf8'),shell=True,stdout=subprocess.PIPE)
        cmd_result = obj.stdout.read()
        conn.sendall(cmd_result)    #一次发送不完所有结果,改为sendall试试

sk.close()

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/cmd_server.py
<socket.socket fd=3, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('0.0.0.0', 0)>
waiting......

cmd_client.py

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:cmd_client.py
# time:2021/03/15

import socket

sk = socket.socket()
print(sk)

address = ('127.0.0.1',8000)
sk.connect(address)    #连接127.0.0.1:8000端口

while True:
    inp = input('>>>')
    if inp == 'exit':
        break
    sk.send(bytes(inp,'utf8'))

    data = sk.recv(1024)
    print(str(data,'utf8'))

sk.close()    #关闭连接

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/cmd_client.py
<socket.socket fd=3, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('0.0.0.0', 0)>
>>>dir
3menu_latest.py		function_\ parameter.py        PICKLE_text
3menu_new.py		function.py		       polymorphic.py
3menu.py		function_return.py	       __pycache__
对齐.py			function_scope.py	       random_module.py
格式化输出.py		generator.py		       reflex.py
日志记录		hashlib_module.py	       re_module.py
深浅拷贝.py		hello			       s1.py
小重山			i.cfg			       s2.py
小重山2			inherit.py		       serve.py
assignment.py		__init__.py		       set.py
built-in-funcation.py	iterator.py		       SHELVE_test
calculator.py		jingdong_passwd		       SHELVE_test.py
cal.py			JSON_loads.py		       shopping_car_login.py
client			json_test.py		       shopping_cart.py
client.py		JSON_text		       shopping.py
closure.py		JSON_text2		       Singleton_mode.py
cmd_client.py		list_builder.py		       smoke
cmd_server.py		list_lesson.py		       special_members.py
code\ template.py	log			       String.py
configparser_module.py	logging_module.py	       sys_module.py
continue.py		login.py		       test
decrator.py		login
>>>cd
_while.py		       test.log
dictionary.py		member_qualifier_character.py  test.py
encapsulation.py	menu.py			       test.xml
encode_py3.py		module_project		       three_level_menu
example.ini		__name__.py		       time_module.py
exception_handling.py	object_oriented.py	       tuple.py
field.py		os_module.py		       weixin_passwd
file_operation2.py	output.xml		       xml_test
file_operation.py	PICKLE_load.py		       XML_test.py
funcation_recursion.py	PICKLE_test.py		       xmltest.xml

>>>ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp4s0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN group default qlen 1000
    link/ether 44:8a:5b:f1:bd:c5 brd ff:ff:ff:ff:ff:ff
3: wlp5s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether d8:fc:93:0f:85:93 brd ff:ff:ff:ff:ff:ff
    inet 192.168.254.20/27 brd 192.168.254.31 scope global dynamic noprefixroute wlp5s0
       valid_lft 4011sec preferred_lft 4011sec
    inet6 fe80::65c8:65dd:a44d:f28c/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

>>>cd    #还是有问题,1、当执行cd命令时候cmd_serve.py返回为空,导致cmd_client.py命令卡住,2、接受时候接收不完导致下一个命令还输出的是上一个命令的返回

cmd_serve.py

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:cmd_server.py
# time:2021/03/15

import subprocess

import socket

# family type
sk = socket.socket()    #两个参数,family=AF_INET代表ipv4,type=SOCK_STREAM代表tcp,proto=0

print(sk)

address=('127.0.0.1',8000)     #ip和端口
sk.bind(address)    #绑定ip和端口
sk.listen(3)    #3代表tcp/udp连接数

print('waiting......')

while True:

    conn, addr = sk.accept()  # 阻塞,accept接收连接内容
    print(addr)
    while True:
        try:
            data = conn.recv(1024)
        except Exception:
            break
        if not data: break
        print('......', str(data, 'utf8'))
        obj = subprocess.Popen(str(data,'utf8'),shell=True,stdout=subprocess.PIPE)
        cmd_result = obj.stdout.read()
        print(cmd_result)

        if not cmd_result:
            return_command = (str(data), 'command exec ok')
            conn.sendall(bytes(' '.join(return_command),'utf8'))
        conn.sendall(cmd_result)

sk.close()

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/cmd_server.py
<socket.socket fd=3, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('0.0.0.0', 0)>
waiting......

cmd_client.py 

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:cmd_client.py
# time:2021/03/15

import socket

sk = socket.socket()
print(sk)

address = ('127.0.0.1',8000)
sk.connect(address)    #连接127.0.0.1:8000端口

while True:
    inp = input('>>>')
    if inp == 'exit':
        break
    sk.send(bytes(inp,'utf8'))

    data = sk.recv(4096)    #修改客户端接收信息长度,但是长支持8K,所以如果更长还是存在问题
    print(str(data,'utf8'))

sk.close()    #关闭连接

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/cmd_client.py
<socket.socket fd=3, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('0.0.0.0', 0)>
>>>dir
3menu_latest.py		function_\ parameter.py        PICKLE_text
3menu_new.py		function.py		       polymorphic.py
3menu.py		function_return.py	       __pycache__
对齐.py			function_scope.py	       random_module.py
格式化输出.py		generator.py		       reflex.py
日志记录		hashlib_module.py	       re_module.py
深浅拷贝.py		hello			       s1.py
小重山			i.cfg			       s2.py
小重山2			inherit.py		       serve.py
assignment.py		__init__.py		       set.py
built-in-funcation.py	iterator.py		       SHELVE_test
calculator.py		jingdong_passwd		       SHELVE_test.py
cal.py			JSON_loads.py		       shopping_car_login.py
client			json_test.py		       shopping_cart.py
client.py		JSON_text		       shopping.py
closure.py		JSON_text2		       Singleton_mode.py
cmd_client.py		list_builder.py		       smoke
cmd_server.py		list_lesson.py		       special_members.py
code\ template.py	log			       String.py
configparser_module.py	logging_module.py	       sys_module.py
continue.py		login.py		       test
decrator.py		login_while.py		       test.log
dictionary.py		member_qualifier_character.py  test.py
encapsulation.py	menu.py			       test.xml
encode_py3.py		module_project		       three_level_menu
example.ini		__name__.py		       time_module.py
exception_handling.py	object_oriented.py	       tuple.py
field.py		os_module.py		       weixin_passwd
file_operation2.py	output.xml		       xml_test
file_operation.py	PICKLE_load.py		       XML_test.py
funcation_recursion.py	PICKLE_test.py		       xmltest.xml

>>>cd
b'cd' command exec ok
>>>ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp4s0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN group default qlen 1000
    link/ether 44:8a:5b:f1:bd:c5 brd ff:ff:ff:ff:ff:ff
3: wlp5s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether d8:fc:93:0f:85:93 brd ff:ff:ff:ff:ff:ff
    inet 192.168.254.20/27 brd 192.168.254.31 scope global dynamic noprefixroute wlp5s0
       valid_lft 4168sec preferred_lft 4168sec
    inet6 fe80::65c8:65dd:a44d:f28c/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

>>>cd
b'cd' command exec ok
>>>

cmd_serve.py

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:cmd_server.py
# time:2021/03/15

import subprocess

import socket

# family type
sk = socket.socket()    #两个参数,family=AF_INET代表ipv4,type=SOCK_STREAM代表tcp,proto=0

print(sk)

address=('127.0.0.1',8000)     #ip和端口
sk.bind(address)    #绑定ip和端口
sk.listen(3)    #3代表tcp/udp连接数

print('waiting......')

while True:

    conn, addr = sk.accept()  # 阻塞,accept接收连接内容
    print(addr)
    while True:
        try:
            data = conn.recv(1024)
        except Exception:
            break
        if not data: break
        print('......', str(data, 'utf8'))
        obj = subprocess.Popen(str(data,'utf8'),shell=True,stdout=subprocess.PIPE)
        cmd_result = obj.stdout.read()    #bytes类型
        print(cmd_result)

        result_len = bytes(str(len(cmd_result)),'utf8')
        print('>>>>>>',result_len)
        conn.sendall(result_len)
        conn.sendall(cmd_result)

sk.close()

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/cmd_server.py
<socket.socket fd=3, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('0.0.0.0', 0)>
waiting......

cmd_client.py

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:cmd_client.py
# time:2021/03/15

import socket

sk = socket.socket()
print(sk)

address = ('127.0.0.1',8000)
sk.connect(address)    #连接127.0.0.1:8000端口

while True:
    inp = input('>>>')
    if inp == 'exit':
        break
    sk.send(bytes(inp,'utf8'))
    result_len = int(str(sk.recv(1024),'utf8'))    #拿到数据大小
    print(result_len)
    data = bytes()    #空bytes,类似data=0
    while len(data) != result_len:    # or result_len != 0
        recv = sk.recv(1024)
        data += recv

    print(str(data,'utf8'))

sk.close()    #关闭连接

/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/cmd_client.py
<socket.socket fd=3, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('0.0.0.0', 0)>
>>>ls
1242
3menu_latest.py
3menu_new.py
3menu.py
对齐.py
格式化输出.py
日志记录
深浅拷贝.py
小重山
小重山2
assignment.py
built-in-funcation.py
calculator.py
cal.py
client
client.py
closure.py
cmd_client.py
cmd_server.py
code template.py
configparser_module.py
continue.py
decrator.py
dictionary.py
encapsulation.py
encode_py3.py
example.ini
exception_handling.py
field.py
file_operation2.py
file_operation.py
funcation_recursion.py
function_ parameter.py
function.py
function_return.py
function_scope.py
generator.py
hashlib_module.py
hello
i.cfg
inherit.py
__init__.py
iterator.py
jingdong_passwd
JSON_loads.py
json_test.py
JSON_text
JSON_text2
list_builder.py
list_lesson.py
log
logging_module.py
login.py
login_while.py
member_qualifier_character.py
menu.py
module_project
__name__.py
object_oriented.py
os_module.py
output.xml
PICKLE_load.py
PICKLE_test.py
PICKLE_text
polymorphic.py
__pycache__
random_module.py
reflex.py
re_module.py
s1.py
s2.py
serve.py
set.py
SHELVE_test
SHELVE_test.py
shopping_car_login.py
shopping_cart.py
shopping.py
Singleton_mode.py
smoke
special_members.py
String.py
sys_module.py
test
test.log
test.py
test.xml
three_level_menu
time_module.py
tuple.py
weixin_passwd
xml_test
XML_test.py
xmltest.xml

>>>hostname
23
smoke-GS70-2PC-Stealth

>>>