在 Linux 上使用 Python 以编程方式提供辅助 WiFi 热点凭据

Programmatically Provide Secondary WiFi HotSpot Credentials with Python on Linux

我有一个 WiFi 服务的凭据 "Open" 从某种意义上说,为了与之关联,用户不需要提供密码。实际访问互联网需要一种辅助身份验证方法,为此我必须通过浏览器输入我的凭据,如下面的屏幕截图所示:

由于我在 Linux,我希望能够使用 Python 以编程方式提供我的凭据。我见过的大多数示例(例如 How to programmatically log into website in Python)都需要 'name of the form'。在浏览器中查看 'Page Source' 时,我看不到此信息,而且我不确定如何正确地自动提交我的凭据。我将不胜感激一些帮助!提前致谢。

更新:这是我试过的一个例子 -

#!/usr/bin/python

import cookielib 
import urllib2 
import mechanize 

# Browser 
br = mechanize.Browser() 

# Enable cookie support for urllib2 
cookiejar = cookielib.LWPCookieJar() 
br.set_cookiejar( cookiejar ) 

# Broser options 
br.set_handle_equiv( True ) 
br.set_handle_gzip( True ) 
br.set_handle_redirect( True ) 
br.set_handle_referer( True ) 
br.set_handle_robots( False ) 

# ?? 
br.set_handle_refresh( mechanize._http.HTTPRefreshProcessor(), max_time = 1 ) 

br.addheaders = [ ( 'User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1' ) ] 

# authenticate 
br.open( "http://www.google.com" ) 
br.select_form( name="Authentication Required" ) 
# these two come from the code you posted
# where you would normally put in your username and password
br[ "USERID" ] = my_user
br[ "PASSWDTXT" ] = my_pass
res = br.submit() 

print "Success!\n"

问题是我刚刚收到这个错误:

Traceback (most recent call last):
  File "./login.py", line 29, in <module>
    br.open( "http://www.google.com" ) 
  File "/usr/local/lib/python2.7/dist-packages/mechanize/_mechanize.py", line 203, in open
    return self._mech_open(url, data, timeout=timeout)
  File "/usr/local/lib/python2.7/dist-packages/mechanize/_mechanize.py", line 255, in _mech_open
    raise response
mechanize._response.httperror_seek_wrapper: HTTP Error 401: Unauthorized

更新 2:我也试过这个:

#!/usr/bin/python

import urllib2

username = my_user
password = my_pass

proxy = urllib2.ProxyHandler({'http': 'http://%s:%s@google.com'%(username,password)})
auth = urllib2.HTTPBasicAuthHandler()
opener = urllib2.build_opener(proxy, auth, urllib2.HTTPHandler)
urllib2.install_opener(opener)

conn = urllib2.urlopen('http://google.com')
return_str = conn.read()

但是错误是一样的:

Traceback (most recent call last):
  File "./login.py", line 13, in <module>
    conn = urllib2.urlopen('http://python.org')
  File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 410, in open
    response = meth(req, response)
  File "/usr/lib/python2.7/urllib2.py", line 523, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.7/urllib2.py", line 448, in error
    return self._call_chain(*args)
  File "/usr/lib/python2.7/urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 531, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 401: Unauthorized

您请求的页面回复了 401 HTTP 响应代码(访问被拒绝),并且您的 Web 浏览器显示 HTTP 基本身份验证 (RFC 2617) 对话框以使用凭据重试请求。

您的第二个示例已完成,但您没有向密码管理器(auth,由 HTTPBasicAuthHandler 创建)提供用户名或密码。尝试这样的事情(Python 2.7):

import urllib2

url = "http://www.example.com"
username = "bob"
password = "pass123"

# Create a password manager with your password(s)
password_store = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_store.add_password(None, url, username, password)
passwords = urllib2.HTTPBasicAuthHandler(password_store)

# Open the URL with password(s) with `opener`
opener = urllib2.build_opener(passwords)
resp = opener.open(url)
resp.read()