Python 2.7 Windows 脚本无法在 Unix 中运行
Python 2.7 Windows scripts not working in Unix
我有一个 python 脚本,我想在 Redhat 6.7 运行 OS 中使用它,但它总是失败。
**Python 版本:2.7.13(最初它有默认版本,我有指向 usr/local/bin/python2.7 的符号链接,不确定它是否已更改为 2.7,但是当我输入时python 在终端中显示位置 usr/local/bin/python。)
脚本 运行 在:OS = Redhat 6.7
脚本编写于:OS = Windows10(python ver 2.7.11)
代码:
import urllib
import json
url = 'https://username:pass@api.amsterdampc.com'# sample URL(tested on 'api.openweathermap.org/data/2.5/weather?q=London' too gives the same error)
data = json.load(urllib.urlopen(url)) #should return a json data
print data
此处打印数据增加 "json decoder error",当我回顾步骤时发现 urllib.urlopen(url) 根本没有返回所需的 json数据而不是一些 ml response/empty 有时。
如果我 运行 不同 OS 中的 python 脚本不是 python 平台独立语言,我需要做任何特定的更改吗?
总的来说,python 合理地 平台无关。但这并不意味着平台之间没有差异。如果您查看标准库的文档,您会发现某些功能或 类 仅在特定平台上可用的注释。例如multiprocessing
类 UNIX 操作系统和 ms-windows.
的工作方式也不同
在这种情况下,您提到问题始于 urllib.urlopen
与您期望的 return 不符这一事实。这可能不是 Python 代码的问题。我 怀疑 这是一个 networking/routing/firewall 问题。您必须显示 returned not-JSON 数据才能确定。
顺便说一句,如果你想在Python中使用HTML,帮自己一个忙,使用requests
模块。它比 urllib
.
更人性化
编辑 1:
它说:
Your request could not be processed. Request could not be handled
This could be caused by a misconfiguration, or possibly a malformed request.
所以有两个可能的原因:
- 配置错误
- 格式错误的请求
由 urllib.urlopen()
编辑的网络对象 return 与文件相比有一些额外的方法,例如 info()
和 getcode()
。使用这些可能会产生一些关于请求失败原因的额外信息。
如果您执行 POST 请求,则必须以特定方式对信息进行格式化和编码。如果您使用 requests.post
,它会为您处理这些细节。
我有一个 python 脚本,我想在 Redhat 6.7 运行 OS 中使用它,但它总是失败。
**Python 版本:2.7.13(最初它有默认版本,我有指向 usr/local/bin/python2.7 的符号链接,不确定它是否已更改为 2.7,但是当我输入时python 在终端中显示位置 usr/local/bin/python。)
脚本 运行 在:OS = Redhat 6.7
脚本编写于:OS = Windows10(python ver 2.7.11)
代码:
import urllib
import json
url = 'https://username:pass@api.amsterdampc.com'# sample URL(tested on 'api.openweathermap.org/data/2.5/weather?q=London' too gives the same error)
data = json.load(urllib.urlopen(url)) #should return a json data
print data
此处打印数据增加 "json decoder error",当我回顾步骤时发现 urllib.urlopen(url) 根本没有返回所需的 json数据而不是一些 ml response/empty 有时。
如果我 运行 不同 OS 中的 python 脚本不是 python 平台独立语言,我需要做任何特定的更改吗?
总的来说,python 合理地 平台无关。但这并不意味着平台之间没有差异。如果您查看标准库的文档,您会发现某些功能或 类 仅在特定平台上可用的注释。例如multiprocessing
类 UNIX 操作系统和 ms-windows.
在这种情况下,您提到问题始于 urllib.urlopen
与您期望的 return 不符这一事实。这可能不是 Python 代码的问题。我 怀疑 这是一个 networking/routing/firewall 问题。您必须显示 returned not-JSON 数据才能确定。
顺便说一句,如果你想在Python中使用HTML,帮自己一个忙,使用requests
模块。它比 urllib
.
编辑 1:
它说:
Your request could not be processed. Request could not be handled
This could be caused by a misconfiguration, or possibly a malformed request.
所以有两个可能的原因:
- 配置错误
- 格式错误的请求
由 urllib.urlopen()
编辑的网络对象 return 与文件相比有一些额外的方法,例如 info()
和 getcode()
。使用这些可能会产生一些关于请求失败原因的额外信息。
如果您执行 POST 请求,则必须以特定方式对信息进行格式化和编码。如果您使用 requests.post
,它会为您处理这些细节。