使用手机将覆盆子pi连接到Wifi

时间:2017-06-28 05:39:54

标签: php python linux raspberry-pi raspberry-pi2

我正在使用Raspberry pi 2 Model B

我想通过手机将我的覆盆子pi连接到检测到的wifi网络。

在使用手机连接到Raspberry pi之后,我可以通过在网址中输入ip地址来访问raspberry pi的网络服务器。

我目前能够在覆盆子pi的网络服务器上扫描检测到的wifi,如下所示:

enter image description here

enter image description here

用于扫描wifi的Python代码:

[1 .. 9]

HTML代码:

#!/usr/bin/env python
import subprocess
import re

class line_matcher:
    def _init_(self, regexp, handler):
        self.regexp  = re.compile(regexp)
        self.handler = handler


def handle_new_network(line, result, networks):
# group(1) is the mac address
networks.append({})
networks[-1]['Address'] = result.group(1)

def handle_essid(line, result, networks):
    # group(1) is the essid name
    networks[-1]['ESSID'] = result.group(1)

def handle_quality(line, result, networks):
    # group(1) is the quality value
    # group(2) is probably always 100
    networks[-1]['Quality'] = result.group(1) + '/' + result.group(2)

def handle_unknown(line, result, networks):
    # group(1) is the key, group(2) is the rest of the line
    networks[-1][result.group(1)] = result.group(2)

if _name_ == '_main_':
    proc = subprocess.Popen(['/sbin/iwlist', 'wlan0', 'scan'],
                            stdout=subprocess.PIPE)
    stdout, stderr =  proc.communicate()

    lines = stdout.split('\n')

    networks = []
    matchers = []

    # catch the line 'Cell ## - Address: XX:YY:ZZ:AA:BB:CC'
    matchers.append(line_matcher(r'\s+Cell \d+ - Address: (\S+)',
                                 handle_new_network))

    # catch the line 'ESSID:"network name"
    matchers.append(line_matcher(r'\s+ESSID:"([^"]+)"', 
                                 handle_essid))

    # catch the line 'Quality:X/Y Signal level:X dBm Noise level:Y dBm'
    #matchers.append(line_matcher(r'\s+Quality:(\d+)/(\d+)',
                                 #handle_quality))

    # catch any other line that looks like this:
    # Key:value
    matchers.append(line_matcher(r'\s+([^:]+):(.+)',
                                 handle_unknown))

    # read each line of output, testing against the matches above
    # in that order (so that the key:value matcher will be tried last)
    for line in lines:
        for m in matchers:
            result = m.regexp.match(line)
            if result:
                m.handler(line, result, networks)
                break

    for n in networks:
        x= 'Found network', n['ESSID']
        print x
        # to see the whole dictionary:
        # print n

PHP代码:

<html>

<head>

<title>My Python Website!</title>
</head>

<body>

    <h1><font size="6" color="red" face="verdana">Welcome to my Python-based website!</font></h1>

    <p><b><i><font size="3" color="gray" face="verdana">This is my first Python-based website that is running on my Raspberry Pi!</font></i></b></p>
    <form action="index.php" method="post">
    <input type="submit" value="Scan"/>
    </form>
    </body>

    </html>

0 个答案:

没有答案