Still looking for remote control? Let's see how I write a simple remote control with a dozen lines of python code

Students who are just starting to learn programming may often ask: "What do I use this for? How much does it cost to take out a computer to program a program when shopping for food?"

In fact, this has a lot to do with everyone's knowledge and the language they choose for entry. Most students will choose C language to start programming. Although C language is simple, the process-oriented programming method will always make people feel boring and at a loss.

So let's do something interesting now, a super simple remote control software, okay~ Actually, it's at most a script.

Still looking for remote control?  Let's see how I write a simple remote control with a dozen lines of python code

Since it is a remote control, two functions need to be determined:

1. Remote

That is, to access external hosts (including intranets) through the network, we can use the socket library packaged in Python to realize remote transmission of text, files, videos, etc.

Introduction to Python socket details

2. Control

Control, the simplest understanding is to execute commands, then we can use the os library in Python to execute Linux shell commands.

Python os details

The following is the code, save it as a .py file, and then execute server.py and then client.py.

Server, save as server.py

#!/usr/bin/env python

# -*- coding:utf-8 -*-

import socket

import os

line=socket.socket(socket.AF_INET,socket.SOCK_STREAM)

line.bind(('服务器IP',监听端口))

line.listen(5)

print('waiting commd------->')

while True:

conn,addr=line.accept()

msg=conn.recv(1024)

if msg == 'q':

break

print('get commd:',msg)

result=os.popen(msg).read()

conn.send('result: '+result)

conn.close()

phone.close()

客户端 保存成client.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import socket

while True:

line=socket.socket(socket.AF_INET,socket.SOCK_STREAM)

line.connect(('服务器IP',监听端口))

msg = str(raw_input('please input commd:'))

line.send(str(msg).encode('utf-8'))

data=phone.recv(1024)

print data

line.close()

The execution result is as follows:

Still looking for remote control?  Let's see how I write a simple remote control with a dozen lines of python code
Server execution result

Still looking for remote control?  Let's see how I write a simple remote control with a dozen lines of python code

The result returned by the client after executing the ls command on the server

In addition, the editor has its own learning exchange group (mainly Python). If you want to come to learn, you can add: 719+139+688, whether you are a novice or a big cow, the editor welcomes you, and the editor I will share dry goods in the group from time to time, including a copy of the latest learning materials in 2018 and a zero-based introductory tutorial compiled by the editor. Beginners and advanced friends are welcome.

Still looking for remote control?  Let's see how I write a simple remote control with a dozen lines of python code

Server command log

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324606847&siteId=291194637