gpt4 book ai didi

python - 在 Windows 上运行良好时在 Linux 上运行 Mechanize 脚本时遇到问题

转载 作者:太空宇宙 更新时间:2023-11-04 06:38:45 24 4
gpt4 key购买 nike

事情是这样的,我写了一个 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 之间的 Mechanize 是否存在差异。由于包内容来自同一来源,我的猜测是不,那么是什么导致了这个问题?

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

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 的两个版本是相同的。注意:我观察到,在检查每个请求的 header 时,cookie 未在 Linux 上设置/存储,但在 Windows 上没问题。

最佳答案

我建议您在深入怀疑之前,尝试清除明显的差异:下载 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

关于python - 在 Windows 上运行良好时在 Linux 上运行 Mechanize 脚本时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4556781/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com