0

我正在使用 Reliance Broadband,它具有基于 Web 的登录方式来访问互联网。它每 24 小时注销一次,所以我必须重新登录。

我遇到了一个PYTHON 脚本,它不断检查连接并保持连接有效。

不知何故,当我在安装最新的 Python 版本后在 Windows 上运行它时,出现以下语法错误。

C:\Documents and Settings\CS\Desktop>reliance-login.py

  File "C:\Documents and Settings\CS\Desktop\reliance-login.py", line 48
    if debug: print "Testing"
                            ^
SyntaxError: invalid syntax

谁能检查一下,让我知道到底是什么问题?

=====

感谢 sr2222,我尝试了 2to3,删除所有 + n - 后...我仍然遇到一些错误。

以下是 2to3 之后的脚本。请您在您的机器上运行它n 帮助修复错误。

            #!/usr/bin/env python
            # encoding: utf-8
            """
            # Reliance Login Script for Python 2.x v1.0
            #
            # Copyright (c) 2009 Kunal Dua, http://www.kunaldua.com/blog/?p=330
            # Copyright (c) 2012 Anoop John, http://www.zyxware.com
            #
            # This program is free software; you can redistribute it and/or modify
            # it under the terms of the GNU General Public License as published by
            # the Free Software Foundation; version 2 of the License.
            #
            # This program is distributed in the hope that it will be useful,
            # but WITHOUT ANY WARRANTY; without even the implied warranty of
            # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
            # GNU General Public License for more details.
            """

            import urllib.request, urllib.error, urllib.parse, urllib, http.cookiejar, time, re, sys

            username = 'username'
            password = 'password'
                 req_headers = {
                     'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; U; ru; rv:5.0.1.6) Gecko/20110501 Firefox/5.0.1 Firefox/5.0.1'
                 }
            request = urllib.request.Request(url, headers=req_headers)
                 if not opener:
                    jar = http.cookiejar.FileCookieJar("cookies")
                    opener = urllib.request.build_opener(urllib2.HTTPCookieProcessor(jar))
                 response = opener.open(request, data)
                 code = response.code
                 headers = response.headers

             def is_internet_on():
                 '''test if the machine is connected to the internet'''
                if debug: print("Testing")
                 try:
                     code, headers, html, opener = get_url('http://74.125.113.99', timeout=10)
                    if debug: print(html)
                     if re.search('google.com', html):
                         return True
                     else:
                         return False
                 except:
                    if debug: print("Error")
                     return False
                 return False

             def internet_connect():
                 '''try to connect to the internet'''
                 code, headers, html, cur_opener = get_url("http://10.239.89.15/reliance/startportal_isg.do", timeout=10)
                if debug: print(html)
                login_data = urllib.parse.urlencode({'userId' : username, 'password' : password, 'action' : 'doLoginSubmit'})
                 code, headers, html, cur_opener = get_url('http://10.239.89.15/reliance/login.do', data=login_data, opener=cur_opener)
                if debug: print(html)

             def internet_disconnect():
                 '''try to disconnect from the internet'''
                 code, headers, html, cur_opener = get_url('http://10.239.89.15/reliance/login.do', timeout=10)
                if debug: print(html)
                 code, headers, html, cur_opener = get_url('http://10.239.89.15/reliance/logout.do', opener=cur_opener)
                if debug: print(html)

             def internet_keep_alive():
                 '''login and keep the connection live'''
                 while True:
                     if not is_internet_on():
                         internet_connect()
                        if debug: print("Not connected")
                     else:
                        if debug: print("Connected")
                         pass
                     time.sleep(check_interval)

             def print_usage():
                print("Reliance Netconnect AutoLogin")
                print("-----------------------------")
                print("usage:" + sys.argv[0] + " [login|logout]\n")
                print("If there are no arguments it runs in an infinite loop and will try to remain connected to the internet.")

             keep_alive = True
             if (len(sys.argv) > 1): 
4

1 回答 1

0

你下载的是 Python 2 还是 Python 3?那是 Python 2.X 打印语法,如果你有 Python 3 解释器,你会看到那个错误。该脚本可能是用 Python 2.X 编写的。您可以下载不同的解释器或在您的保持活动脚本的源上运行 2to3.py 脚本,以使其在 Python 3 下工作。

另外,哇,还有 ISP 仍然这样做吗?对不起,你被困住了。

于 2012-08-02T10:56:56.443 回答