在Linux上运行时,在Linux上运行机械化脚本时遇到问题

时间:2010-12-29 18:32:53

标签: python authentication https mechanize

这就是问题,我编写了一个Python脚本,它连接到我的工作网页并下载我最近的薪水。它在Windows中运行良好但是当我将此脚本移动到运行Debian的服务器时,它无法重定向到初始页面。平台之间有区别吗?现在搜索几个小时并没有为我带来任何有用的信息。我已经确定两个系统和运行相同版本的机械化但Python版本不同(编辑:现在两者都是相同的),虽然只是有点不同。

Both systems are running identical versions of Python and mechanize.
# >> python -V
Python 2.7.1
#mechanize >> print(mechanize.__version__)
(0, 2, 4, None, None)

现在我已经创建了一个用于测试的精简测试脚本。这样我就可以轻松地比较结果。在Windows中,脚本将返回包含所有可用薪水清单的最终页面,而在Linux中,它只会打印应重定向到登录页面的初始页面。我觉得Linux下的机械化根本不是重定向,或者只是没有设置继续进行所需的cookie。

有什么想法吗?建议?我基本上都在询问Windows和Linux之间的机械化是否存在差异。由于包装内容来自同一来源,我的猜测是否定的,但是导致这个问题的是什么?

以下是我用来测试的代码。显然我遗漏了正确的用户名和密码:)

import mechanize;
import urllib;

#constants
URL_OPEN = "https://ep.upsers.com/ep-s/UPSRegistration/UPSLogin";#set a cookie
URL_SECURE = "https://ep.upsers.com/gems-secure/epay_eng.html";
URL_PAYCHECK = "https://ep.upsers.com/gems-secure/psc/hrprod/EMPLOYEE/HRMS/c/M_UPS_MENU.VW_PYCHK_M.GBL?Page=PYCHKDAT_M&Action=U";#lists paychecks
VIEWALL = "#ICViewAll";


def testConnection(username, password):
    success = "no connection: ";

    try:
        #get a cookie to use later
        mechanize.HTTPSHandler();
        request1 = mechanize.Request(URL_OPEN);
        response1 = mechanize.urlopen(request1);

        #attempt our login
        postdata = {"user": username,"password": password};
        post = urllib.urlencode(postdata);
        headers =  {"User-agent" : "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)"};
        request2 = mechanize.Request(URL_OPEN, post, headers);
        response2 = mechanize.urlopen(request2);

        #navigate to paycheck page
        request3 = mechanize.Request(URL_PAYCHECK);
        response3 = mechanize.urlopen(request3);

        success = response3.read();

    except Exception as ex:
        success += str(ex);

    print(success);
#end testConnection

testConnection('USERNAME', 'PASSWORD')

最初我认为用户代理可能需要更改,但结果没有变化。

注意:现在两个版本的Python和机械化都是相同的。 注意:我观察到,在检查每个请求的标头时,没有在Linux上设置/存储cookie,但是在Windows中它很好。

2 个答案:

答案 0 :(得分:0)

我建议在你追踪深层嫌疑人之前,尝试清除明显的差异:下载Python 2.7(无需安装)并查看行为是否与Windows匹配。

这是'构建并运行2.7'copy-n-paste:

wget http://python.org/ftp/python/2.7.1/Python-2.7.1.tgz
tar -xzf Python-2.7.1.tgz
cd Python-2.7.1/
./configure && make
./python /path/to/your/script

答案 1 :(得分:0)

哪个用户在服务器上执行脚本以及它具有哪些权限?

您同时对执行环境进行了多处更改:

  1. Python版。
  2. 操作系统。
  3. 用户
  4. 用户环境(具有主目录的用户不同)。
  5. 尝试一次将其缩小到一个更改。在Windows机器上通过虚拟化Linux进行测试将是一个好主意。