【发布时间】:2012-06-09 11:27:15
【问题描述】:
我正在开发一个名为RubberBand 的开源项目,它是一个开源项目,允许您按照标题所说的去做。在本地执行位于 Web 服务器上的 python 文件,但是我遇到了问题。如果逗号位于字符串中(例如“http:”),则会返回错误。
'''
RubberBand Version 1.0.1 'Indigo-Charlie'
http://www.lukeshiels.com/rubberband
CHANGE-LOG:
Changed Error Messages.
Changed Whole Code Into one function, rather than three.
Changed Importing required libraries into one line instead of two
'''
#Edit Below this line
import httplib, urlparse
def executeFromURL(url):
if (url == None):
print "!# RUBBERBAND_ERROR: No URL Specified #!"
else:
CORE = None
good_codes = [httplib.OK, httplib.FOUND, httplib.MOVED_PERMANENTLY]
host, path = urlparse.urlparse(url)[1:3]
try:
conn = httplib.HTTPConnection(host)
conn.request('HEAD', path)
CORE = conn.getresponse().status
except StandardError:
CORE = None
if(CORE in good_codes):
exec(url)
else:
print "!# RUBBERBAND_ERROR: File Does Not Exist On WEBSERVER #!"
【问题讨论】:
-
你可以在一行 shell 代码中做到这一点:
wget http://some-url.com/path | python. -
@Thomas - 几乎:
wget -O - http://some-url.com/path | python -
有比
httplib更高级别的API 用于通过HTTP 获取文件,例如requests或urllib2。请参阅下面我的答案中的示例。