您的位置:首页 > 编程语言 > Python开发

手机控制电脑的一种思路(Python实现)

2015-06-07 17:24 856 查看

最近上网换了个运营商,只能插有线。于是手机自然就只能接受笔记本的热点了。但是晚上上床玩手机时电脑不关,玩到3、4点再下去关未免太麻烦了。

为了解决这个问题,我想到了两个办法:

1.不玩手机了

2.编写一个可以在手机和电脑处于同一局域网时从手机控制电脑的程序(虽然各种WiFi工具已经有实现,我也没去了解过他的原理,只想自己写个最轻量,功能最核心的)

首先我的思路是基于HTTP协议,先写个Socket程序监听某个端口运行在电脑上,再从手机访问PC的局域网IP,发送请求参数,PC接受参数之后,运行相应cmd命令即可。

于是整个系统分为两部分:

PC上:socket程序

手机上:web app

socket我采用的是Django,因为最近在学Django所以就没用socket模块。

配置好Django的开发环境之后,修改urls.py,配置url映射:

<span style="font-size:14px;">from django.conf.urls import include, url
from django.contrib import admin
from RCtrlServer.ctrls import handler
from RCtrlServer.ctrls import ui
from django.conf import settings

urlpatterns = [
url(r'^ctrl/$', handler.handle_request),
url(r'^main/$', ui.show),
url(r'^admin/', include(admin.site.urls)),

url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}), # static file
]</span>


其中main/映射用于给用户返回UI模板,ui.py如下:

<span style="font-size:14px;">from django.shortcuts import render_to_response

def show(request):
return render_to_response('main.html')</span>


main.html代码贴在最后,贴一张手机上的效果图:



按钮依次是关机、注销、锁屏、退出。

ctrl/用于处理用户操作,handler.py如下:

<span style="font-size:14px;">from django.shortcuts import render_to_response
import os

def handle_request(request):
print('Request: ' + request.get_full_path())
op = request.GET['op']
time = request.GET['time']
cmd = 'cmd /c '
if op == 'lock':
cmd += 'rundll32.exe user32.dll,LockWorkStation'
elif op == 'l':
cmd += 'shutdown /l'
else:
time = '0' if time == '' else time
cmd += 'shutdown /f /s /t ' + time

print('PC will execute: ' + cmd)
os.system(cmd)
return render_to_response('none.html')</span>


主要用来获取用户发送的参数,执行对应cmd语句。

接下来我们来写安卓APP。

因为是基于HTTP协议,所以我打算写个纯WebApp,直接用WebView把你局域网下main.html的地址作为loadUrl()的参数,比如我的局域网地址为192.168.155.1,所以编写MainActivity.java如下:

<span style="font-size:14px;">package com.cymurus.remotex;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {
private WebView webview;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

webview = new WebView(this);
webview.loadUrl("http://192.168.155.1:8000/main/");

WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);

webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});

setContentView(webview);
}

}
</span>


可以通过以下方法查看局域网ip

cmd->ipconfig /all

找到 无线局域网适配器 本地连接* xx 的IP即为所求。

之后就可以用手机控制电脑了,各位也可以去拓展一下参数,增加一些cmd命令,实现不同的控制效果。

*注意:

1.Socket不一定要用Django,更不一定要用Python。能支持Socket编程的语言都可以实现。

2.这种方式很不安全(特别适用Django)。在同一个局域网其他人,一旦知道了你的接口,也能控制你的电脑。就本例而言可以加入身份认证机制。

3.对于有些Android手机的WebView不支持window.location.href的解决方法参看这里。

最后,main.html的代码:
<span style="font-size:14px;"><!DOCTYPE html>
<html lang="en">
<head>
<title>Main</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1 user-scalable=0"/>
<link rel="stylesheet" href="/static/lib/bootstrap/css/bootstrap.min.css">
<style>
.container {
width: 90%;
margin: 0 auto;
}

.icon {
font-size: 50px;
padding: 30px;
}
</style>
<script>
function XHR() {
// 参考于 http://www.cnblogs.com/gaojun/archive/2012/08/11/2633891.html var xhr;
try {xhr = new XMLHttpRequest();}
catch(e) {
var IEXHRVers =["Msxml3.XMLHTTP","Msxml2.XMLHTTP","Microsoft.XMLHTTP"];
for (var i=0,len=IEXHRVers.length;i< len;i++) {
try {xhr = new ActiveXObject(IEXHRVers[i]);}
catch(e) {continue;}
}
}
return xhr;
}

function doAction(btn){
var xhr = XHR();
var url = '/ctrl/?op=' + btn.id + '&time=' + document.getElementById('time').value;
xhr.open("get", url, true);
xhr.send(null);
return false;
}
</script>
</head>
<body style="background-color: #2c3e50; margin-top: 100px;">
<div class="container" align="center">
<form action="" id="ctrl_form">
<div class="row form-group">
<input type="number" id="time" class="form-control input-lg" style="margin: 0 auto; font-size: 20px;" placeholder="time ( not required. Unit: s)">
</div>
<div class="row form-group">
<button id="s" class="btn btn-danger btn-lg col-md-6" onclick="doAction(this);"><span class="glyphicon glyphicon-off icon"></span></button>  
<button id="l" class="btn btn-warning btn-lg col-md-6" onclick="doAction(this);"><span class="glyphicon glyphicon-log-out icon"></span></button>
</div>
<div class="row form-group">
<button id="lock" class="btn btn-success btn-lg col-md-6" onclick="doAction(this);"><span class="glyphicon glyphicon-lock icon"></span></button>  
<button id="exit" class="btn btn-default btn-lg col-md-6"><span class="glyphicon glyphicon-remove icon"></span></button>
</div>
</form>
</div>
</body>
</html></span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: