0

This questing might seem a bit dumb since I'm only beginning to get a grasp of HTTP. As far as I understand, HTTP is a communication protocol, which means that 2 computers can communicate using it. On the internet, however, it seems like all HTTP servers are serving files. Can I make a flask server which doesn't serve files but just communicates with clients over HTTP?

micstr
  • 5,080
  • 8
  • 48
  • 76
  • Are you asking if you can just return text? – Nick Chapman Jul 06 '17 at 08:51
  • Kind of, i want to send text which is not in a file but in a variable for example. – PePePlusPlus Jul 06 '17 at 11:46
  • 1
    You may find this series of posts interesting if you want to learn about how web servers work. Uses Python for examples. https://ruslanspivak.com/lsbaws-part1/ – Graham Dumpleton Jul 06 '17 at 12:09
  • Yeah you are right i confused pages and files over there, the question is indeed absurd now, but i got a solution which allows me to solve the problem another way so i think i will either delete or edit this question. – PePePlusPlus Jul 06 '17 at 15:38

1 Answers1

1

Can I make a flask server which doesn't serve files but just communicates with clients over HTTP?

My understanding is that when you receive a "file" in a "normal" web request, what's really happening is that you are receiving an HTTP response (which is like a text file), and that HTTP response contains a status code (which is what you seem to be thinking of as the real HTTP), a "content" section which contains the text of the "file", and there is another line in the HTTP response that specifies how the recipient computer should interpret the contents of that "content" section (e.g. as JSON, or plaintext, or HTML, or XML, etc.). That line usually looks something like Content-Type: text/html.

In your case, you would only be specifying the status code while leaving the content section blank.

Since you aren't sending any content, you might be thinking that you'd like to specify a 'null' content type in your HTTP responses, but there doesn't seem to be a 'null' content type. Nevertheless, it wouldn't really matter what you set the content-type to since you wouldn't be filling out any content. One thing I've seen is to set it to text/plain or application/json.


If you want to send structured data to the user, the usual way to do that is to send it as JSON. So you would specify that Content-Type as application/json, and then you would format the body ("content") of your response as JSON.

Here's an example of Flask code that would do this:

@app.route('/get_current_user')
def get_current_user():
    return jsonify(
        username=g.user.username,
        email=g.user.email,
        id=g.user.id
    )

Source: Return JSON response from Flask view

Nathan Wailes
  • 9,872
  • 7
  • 57
  • 95
  • Thanks, but that solves only a part of the problem, how do I send data without it being in a file. – PePePlusPlus Jul 06 '17 at 11:48
  • I've updated my answer to include a description of how to send structured data via HTTP requests. The usual way to do this is to format the data as JSON. – Nathan Wailes Jul 06 '17 at 12:08