要从我的Raspberry Pi连接到网络,我使用this python library。我的问题是我不断得到ConnectionError,即使我在try / except块中处理它,或者至少我认为我正在处理它。 该程序扫描无线网络的空中接口,然后排除没有匹配前缀的网络,然后按信号质量对匹配的网络进行排序。之后,connectToGateway()尝试连接到最好的一个,如果失败,它会在列表中查找第二个,依此类推。如果以前保存了某个网络的方案,它会重用它(这是AssertionError异常处理的内容)。 我的代码:
from wifi import Cell, Scheme
from collections import namedtuple
from operator import attrgetter
from wifi.exceptions import ConnectionError
from optparse import OptionParser
class NetworkConnection:
def __init__(self, prefix, interface, password):
self.prefix = prefix
self.interface = interface
self.password = password
def discover_avalible_networks(self, interface=None):
all_networks = []
interface = self.interface
avalible_networks = Cell.all(interface)
for network in avalible_networks:
all_networks.append(network)
return all_networks
def select_appropriate_networks(self, prefix=None):
appropriate_network = namedtuple('network', 'ssid quality encrypted encryption_type')
appropriate_networks = []
prefix = self.prefix
for network in self.discover_avalible_networks():
if network.ssid.startswith(prefix):
appropriate_networks.append(
appropriate_network(network.ssid, network.quality, network.encrypted, network.encryption_type))
return appropriate_networks
def sort_appropriate_networks(self):
unsorted_appropriate_networks = self.select_appropriate_networks()
sorted_appropriate_networks = sorted(unsorted_appropriate_networks, key=attrgetter('quality'), reverse=True)
return sorted_appropriate_networks
def connect_to_gateway(self, interface=None, password=None):
interface = self.interface
password = self.password
networks = self.sort_appropriate_networks()
for network in networks:
try:
print("Trying to connect to {}").format(network.ssid)
scheme = Scheme.for_cell(interface, network.ssid, network, password)
scheme.save()
scheme.activate()
print("Connected to {}").format(network.ssid)
break
except AssertionError:
scheme = Scheme.find(interface, network.ssid)
scheme.activate()
print("Connected to {}").format(network.ssid)
break
except ConnectionError:
print("Couldn't connect to {}").format(network.ssid)
scheme = Scheme.find(interface, network.ssid)
scheme.delete()
continue
if __name__ == "__main__":
parser = OptionParser()
parser.add_option('-p', '--prefix', dest='prefix', help="Specify gateway ssid prefix")
parser.add_option('-i', '--interface', dest='interface', help="Specify name of wireless interface of choice")
parser.add_option('-q', '--password', dest='password',
help="Specify password for the wireless network of choice")
options, args = parser.parse_args()
wireless_connection = NetworkConnection(prefix=options.prefix, interface=options.interface,
password=options.password)
wireless_connection.connect_to_gateway()
所以看起来它没有发现这个错误。
答案 0 :(得分:1)
由于ConnectionError
部分内的scheme.activate()
来电未提出try
,因此scheme.activate()
未被抓住在except AssertionError
部分内的第52行进行wifi
调用。
我认为AssertionError
模块文档告诉你这样抓住AssertionError
,但这有点奇怪。 AssertionError
旨在用于捕获程序中的逻辑错误,IOW,如果程序逻辑正确则应该永远不会发生的事情,它不应该被用来捕获糟糕的数据或糟糕的环境条件。通常情况下,如果程序引发wifi
,则表明程序本身需要修复。
我不熟悉Scheme.find
模块,因此我不知道如何正确解决您的问题。但是代码似乎在激活except AssertionError
部分内try.. except block
调用返回的Scheme时遇到问题。我猜你可以把整个while
放在<div id="body" role="main">
<h1>Title</h1>
<p>My site is mysite.com</p></div>
循环中,最后一小段时间延迟,然后继续循环直到你得到一个有效的连接。