Post Request to web server with Flask
Question: When you run this python script, you will see a feature of network connection
with flask on 127.0.0.1:5000/

But, I could not figure out to print all of features of network from starting
to run script. I mean I'm losing the previous data, so I could print any one
feature of my network to my web server when refreshing my page.

I haven't found anything specific in Flask documentation. Some say urllib2 or
post.request is useful for it, but I'm new to Flask and Python for web at all.

Thanks in advance!

Code:

    
    
    import socket, sys
    from struct import *
    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route('/')
    
    def hello_world():  
    
    s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)
    
    while True:
    
            packet = s.recvfrom(65536)
    
            packet = packet[0]
    
            ip_header = packet[0:20]
    
            iph = unpack('!2B3H2BH4s4s' , ip_header)    
    
            t_length = iph[2] 
            protocol = iph[6]
    
            s_addr = socket.inet_ntoa(iph[8]);
            d_addr = socket.inet_ntoa(iph[9]);
    
            protocol_s = protocol    
    
            if protocol == 1:
                protocol_s = 'ICMP'
            if protocol == 6:
                protocol_s = 'TCP'
            if protocol == 17:
                protocol_s = 'UDP'
    
            tcp_header = packet[20:40]
    
            tcph = unpack('!HHLLBBHHH' , tcp_header)
    
            dest_port = tcph[1]     
    
        test = 'Protocol : ' + protocol_s + ' | Source Address : ' + str(s_addr) + ' | Destination Address : ' + str(d_addr) + ' | Dest Port : ' + str(dest_port) + ' | Packet Length : ' + str(t_length)
    
        return test
    
    if __name__ == '__main__':
        app.run()
    


Answer: First of all, Flask is a WSGI framework. It will only work on TCP/IP and HTTP
is the protocol you will use in _most cases_. You might use websockets and
other protocols, but they will also work over TCP. The socket handling is
already done by the server and you do not have to worry about it.

For getting information about the connection, I'm not sure how much you can
get out. You can take a look at the
[`flask.Request`](http://flask.pocoo.org/docs/api/#incoming-request-data) and
the
[`werkzeug.wrappers.Request`](http://werkzeug.pocoo.org/docs/wrappers/#werkzeug.wrappers.Request)
objects.

For example, you can get the remote address from the `request`:

    
    
    from flask import request
    
    @app.route('/')
    def hello():
        print request.remote_addr
    

