Still looking for a remote control? Look, I'm writing a simple remote control with more than 10 lines of Python code.

Source: Internet
Author: User

students who have just started to learn programming may often ask, "What do I do with this?" How much does it cost to get a computer to make a program when you buy vegetables? "

In fact, this with everyone's knowledge and access to the language of choice has a great relationship, most students choose c language to get started programming, C language, although concise, but the process-oriented programming method always makes people feel dull, at a loss.

Now let's do an interesting, and make a super simple remote control software, OK ~ actually a script at most.

Since it is remote control, it is necessary to determine two functions:

1. Remote

That is, through the network access to external host (including intranet), we can use the Python encapsulated socket library, can be used to remotely transfer text, files, video and so on.

Python Socket Details Introduction

2. Control

Control, the simplest understanding is to execute the command, then we can execute the Linux shell command using the OS library in Python.

Python OS Detail Introduction

Here's the code, save it as a. py file, and then execute server.py before executing client.py.

Server, Save as server.py

#!/usr/bin/env python# -*- coding:utf-8 -*-import socketimport osline=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‘:breakprint(‘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 socketwhile 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 dataline.close()

The results of the implementation are as follows:


Service-Side Execution results

Results returned by the client after executing the LS command on the server

In addition, small series have their own learning Exchange group (mainly Python) if you want to learn, you can add: 719+139+688, whether you are small white or Daniel, small series are welcome, and small in the group will not regularly share dry goods, Including a small series of their own finishing a 2018 of the latest learning materials and 0 Basic introductory tutorial, welcome beginner and Advanced Small partners

Server-side command log

Still looking for a remote control? Look, I'm writing a simple remote control with more than 10 lines of Python code.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.