在 Windows 上运行良好的 mechanize 脚本在 Linux 上运行时遇到问题

发布于 2024-10-09 07:41:21 字数 2116 浏览 2 评论 0原文

事情是这样的,我编写了一个 Python 脚本,它连接到我的作品网页并下载我最近的薪水。它在 Windows 中运行得很好,但是当我将此脚本移动到运行 Debian 的服务器时,它无法重定向到初始页面。各个平台之间有区别吗?现在搜索几个小时并没有为我带来任何有用的信息。我已经确保两个系统都运行相同版本的 mechanize,但 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 下的 mechanize 要么根本不重定向,要么只是没有设置继续操作所需的 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 和 mechanize 的两个版本是相同的。 注意:我观察到,在检查每个请求的标头时,cookie 没有在 Linux 上设置/存储,但在 Windows 中则很好。

Well here is the thing, I have written a Python script that connects to my works webpage and downloads my most recent paycheck. It works great in windows but when I move this script to my server that is running Debian it fails to redirect past the initial page. Is there a difference between the platforms? Searching for hours now has not resulted in any useful information for me. I have made sure that both systems and running the same version of mechanize but the Python versions are different (EDIT: now both are identical), although just a bit different.

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)

Now I have created a stripped down test script just for testing. This is so I can compare the results easily. In Windows the script will return the final page that contains a list of all the paychecks available while in Linux it will only print the initial page that should be redirecting to the login page. I feel that mechanize under Linux is either not redirecting at all or it is just not setting a cookie that is needed to proceed.

Any ideas? suggestions? I am basically asking if there is a difference in mechanize between Windows and Linux. Since the package contents are from the same source my guess is no but then what is causing this issue?

Here is the code that I am using to test. Obviously I have left out the correct username and password :)

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')

Initially I thought that the user-agent might need to changed but there is no change in the results.

NOTE: Now both versions of Python and mechanize are identical.
NOTE: I have observed that while examining the headers at each request that the cookie is not being set/stored on Linux but in Windows it is fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

真心难拥有 2024-10-16 07:41:21

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

这是“构建并运行 2.7”复制粘贴:

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

I'd suggest that before you go after the deep suspects, try clearing the obvious differences: download Python 2.7 (no need to install it) and see if the behavior matches Windows.

Here's a 'build and run 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
最丧也最甜 2024-10-16 07:41:21

哪个用户在服务器上执行该脚本以及它拥有什么权限?

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

  1. Python 版本。
  2. 操作系统。
  3. User
  4. 用户环境(主目录不同的用户)。

尝试将其范围缩小到一次只进行一项更改。在 Windows 计算机上通过虚拟化 Linux 进行测试将是一个好主意。

Which user executes the script on the server and what privileges does it have?

You're several changes to the execution environment at the same time:

  1. Python version.
  2. Operating system.
  3. User
  4. User environment (users with home directories are different).

Try to narrow it to one change at a time. Testing over a virtualized Linux on your Windows machine would be a good idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文