Code

@app.route('/') def parse(ip): theurl = 'http://%s/requests/status.xml' % ip username = '' password = '' passman = urllib2.HTTPPasswordMgrWithDefaultRealm() passman.add_password(None, theurl, username, password) authhandler = urllib2.HTTPBasicAuthHandler(passman) opener = urllib2.build_opener(authhandler) urllib2.install_opener(opener) pagehandle = urllib2.urlopen(theurl) doc = minidom.parse(pagehandle) full = doc.getElementsByTagName("fullscreen") vol = doc.getElementsByTagName("volume") fullscreen = full[0].childNodes[0].nodeValue volume = vol[0].childNodes[0].nodeValue return "fullscreen: %s Volume: %s" % (fullscreen,volume) def index(): parse(ip='значение') 

When executing gives an error

 TypeError: parse() takes exactly 1 argument (0 given) 

Tell me what could be the problem, the value seems to be passed

  • Because of the decorator @app.route('/') probably read about them - OlegUP

2 answers 2

route('/') does not contain any parameters, so when you visit / path there is a call to parse() without parameters as well, which leads to a TypeError .

If you want to make requests like http://ваш сервис/a.example.com , then you must explicitly specify the parameter using <имя параметра> syntax .
Here is a complete example of a web application:

 #!/usr/bin/env python import xml.etree.ElementTree as etree import requests # $ pip install requests from flask import Flask, jsonify # $ pip install flask app = Flask(__name__) @app.route('/', defaults=dict(host='example.com')) @app.route('/<host>') def make_request(host): url = 'http://{host}/requests/status.xml'.format(**vars()) r = requests.get(url, auth=('user', 'password')) doc = etree.fromstring(r.text) return jsonify(fullscreen=doc.findtext('.//fullscreen').strip(), volume=doc.findtext('.//volume').strip()) if __name__ == '__main__': app.run(host='localhost') 

If the host is not specified, then the default example.com used in this example.

To install the dependencies and start the server:

 $ python -m pip install requests flask # install dependencies $ python сервер.py # start http server 

To open a link in a browser:

 $ python -m webbrowser http://localhost:5000/b.example.com # open browser 
  • Thank you. Your example is very good, but I ran into a problem when I try to get the values ​​after parsing, namely "'ascii' codec can't encode characters in position", tried the encode ('utf-8'), tell me where to bathe? (Yes, there are Russian words in the text) - santer
  • @santer: the code works with Russian words (with any Unicode characters in fact). To understand what the problem is: create a minimal , but complete example of the code that demonstrates it, describe the desired behavior in words, describe what happens instead, step by step, if there are error messages, then complete traceback, specify OS, version of Python and publish all together as a separate question. - jfs

And what do your decorator do ??? @app.route('/') Most likely because of it.

UPD

try instead of @app.route('/') :

  • @app.route('/', {'ip' : 'передаете IP'})

or

  • @app.route('/', ip='передаете IP')

In general, here: the link there look on the page description of app.route

P.S. Thanks for the great comment - OlegUP

  • i use flax, so it indicates the route - santer
  • one
    try instead of @app.route('/') - @app.route('/', {'ip' : 'откуда то берете IP'}) or @app.route('/', ip='откуда то берете IP') in common here: look for the link there on the page description app.route - OlegUP