我有两个网络应用程序:parent_app和sub_app
说,http://www.some.com/parent.png将由parent_app处理。
如果它是另一个网站的refererd,则parent_app会获得一个HTTP_REFERER,比如http://www.other.com/path?query=value,
我希望sub_app将此HTTP_REFERER的路径和query_string作为自己的路径和query_string,并将结果返回给parent_app,因此,parent_app的url不会被更改,访问者浏览器也不会获得303跳。 / p>
的 sub.py: 的
import web
class Sub(object):
def GET(self):
return web.input().query # I want it to be 'value', from "query=value"
urls = (r'/path', 'Sub')
sub_app = web.application(urls, locals())
的 parent.py: 的
import web
from sub import sub_app
class Parent(object):
def GET(self):
return sub_app.request('/path?query=value').data #=========(1)
urls = (
r'/parent.png', 'Parent',
r'', sub_app
)
parent_app = web.application(urls, locals())
并运行:
>>>python parent.py
当我访问'http://www.some.com/parent.png'时?
我收到了以下错误:
Traceback (most recent call last):
File "/home/netroyal/Documents/program/studame/web/wsgiserver/__init__.py", line 1245, in communicate
req.respond()
File "/home/netroyal/Documents/program/studame/web/wsgiserver/__init__.py", line 775, in respond
self.server.gateway(self).respond()
File "/home/netroyal/Documents/program/studame/web/wsgiserver/__init__.py", line 2018, in respond
response = self.req.server.wsgi_app(self.env, self.start_response)
File "/home/netroyal/Documents/program/studame/web/httpserver.py", line 306, in __call__
return self.app(environ, xstart_response)
File "/home/netroyal/Documents/program/studame/web/httpserver.py", line 274, in __call__
return self.app(environ, start_response)
File "/home/netroyal/Documents/program/studame/web/application.py", line 279, in wsgi
result = self.handle_with_processors()
File "/home/netroyal/Documents/program/studame/web/application.py", line 249, in handle_with_processors
return process(self.processors)
File "/home/netroyal/Documents/program/studame/web/application.py", line 246, in process
raise self.internalerror()
File "/home/netroyal/Documents/program/studame/web/application.py", line 473, in internalerror
parent = self.get_parent_app()
File "/home/netroyal/Documents/program/studame/web/application.py", line 458, in get_parent_app
if self in web.ctx.app_stack:
AttributeError: 'ThreadedDict' object has no attribute 'app_stack'
/ home / netroyal / Documents / program / studame / web /是web.py包路径。
那么,如何让(1)正确运行?
我希望获得相同的结果,例如(1)在shell中运行:
>>> import web
>>> class Sub(object):
... def GET(self):
... return web.input().query
>>> urls = ('/path', 'Sub')
>>> sub_app = web.application(urls, locals())
>>> sub_app.request('/path?query=value').data #=========(1)'
'value'
>>>
我知道,我可以使用
web.seeother('/path?query=value')
让访问者看到结果,但我不希望浏览器跳转到另一个网址。
我认为
urllib2.urlopen('http://www.some.com/path?query=value')
会有效,但是有更好的方法可以在一个请求中完成吗?
感谢您的帮助!----- 也可以阅读此内容! ==========================================编辑======= ===============================
好的,经过一些代码黑客攻击,我解决了我的一部分问题:
我添加了simulation.py:
import web, re
class Simulation(object):
def __init__(self, urls, fvars):
self._urls = urls
self._fvars = fvars
def request(self, localpart='/', method='GET', data=None, host='0.0.0.0:8080', headers=None, https=False):
#nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
# get path and args(parameters from new url)
try:
path, query = localpart.split('?', 1)
except:
path, query = (localpart, '')
# get all arguments: args(parameters)
parts = query.split('&')
args = {}
for part in parts:
try:
name, value = part.split('=')
except:
pass
else:
args[name] = value
#uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
patterns = [self._urls[2*i] for i in range(0, len(self._urls)/2)]
for i in range(0, len(patterns)):
result = re.match(patterns[i], path)
if result:
web.input = lambda: web.storage(args)
Worker = self._fvars[self._urls[2*i + 1]]
return Worker().GET(*result.groups())
#----------------------------------------------------------------------------------------
raise web.notfound()
并且在 的 sub.py:
from simulation import *
class Sub(object):
def GET(self):
return web.input().query # I want it to be 'value', from "query=value"
urls = (r'/path', 'Sub')
sub_app = web.application(urls, locals())
sub_sim = Simulation(urls, locals()) # new class to run request in parent_app
加入 的 parent.py:
import web
from sub import sub_app, sub_sim #sub_sim is new
class Parent(object):
def GET(self):
return sub_sim.request('/path?query=value') # sub_app changed to sub_sim, and no (.data)
urls = (
r'/parent.png', 'Parent',
r'', sub_app
)
parent_app = web.application(urls, locals())
我也可以使用'/ sub'来访问sub_app,保持它是一个独立的应用程序。
我解决了我的问题,但并不完美,但有些困难。我想我会用它,当我有更多时间时,我会找到另一种方式。 如果您有更好的解决方案,请告诉我,谢谢。
最好的问候。
========================================= 我觉得我在跟自己说话,人们在哪里?