5

I have configured python server using flask framework. Now I want to send request to server from my desktop application. My server location is : http://127.0.0.1:5000/

I have written code which work from browser but i want to access that code from my desktop app.

import flask,flask.views import os import functools 
app=flask.Flask(__name__)

app.secret_key="xyz" users={'pravin':'abc'}
class Main(flask.views.MethodView):
    def get(self):
        return flask.render_template('index.html')
    def post(self):
        if 'logout' in flask.request.form:
            flask.session.pop('username',None)
            return flask.redirect(flask.url_for('index'))
        required=['username','passwd']
        for r in required:
        if r not in flask.request.form:
            flask.flash("Error: {0} is required.".format(r))
            return flask.redirect(flask.url_for('index'))

        username=flask.request.form['username']
        password=flask.request.form['passwd']
        if username in users and users[username]==password:
            flask.session['username']=username
        else:
            flask.flash("Username doesn`t exist or incorrect password.")

        return flask.redirect(flask.url_for('index'))

app.add_url_rule("/",view_func=Main.as_view("index"),methods=["GET","POST"])
app.debug=True
app.run()

2 Answers 2

7

You have to use a python library to access the url from the desktop app.

Try requests module. It makes working with http request easy and describes itself as an elegant and simple HTTP library for Python, built for human beings.

Sample code:

    >>> r = requests.get('http://127.0.0.1:5000')
    >>> r.status_code
        <Response [200]>
Sign up to request clarification or add additional context in comments.

2 Comments

Why do you recommend an external module without a reason?
@BlaXpirit Because its easy to use!
4

Did you try to use the python standard library httplib ?

Or the even better httplib2.

Here is a GET test (inspired by httplib2 examples) :

import httplib2
h = httplib2.Http()
resp, content = h.request("http://127.0.0.1:5000/")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.