开发者

Streaming the state of a Python script to a website

开发者 https://www.devze.com 2023-01-16 11:13 出处:网络
I have a simple workflow [Step 0]->[1]->[2]->...->[Step N]. The master program knows the step (state) it is currently at.

I have a simple workflow [Step 0]->[1]->[2]->...->[Step N]. The master program knows the step (state) it is currently at.

I want to stream this in real time to a website (in the local area network) so that when my colleagues open, say, http://thecomputer:8000, they can see a real time rendering of the current state of our workflow with any relevant details.

I've tought about writing the state of the script to an StringIO object (streaming to it) and use Javascript to refresh the browser, but I hone开发者_开发问答stly have no idea how to actually do this.

Any advice?


You could have the python script write an xml file that you get with an ajax request in your web page, and get the status info from that.


I don't have an idea about how Python works, but if it were PHP this would be easy (if you know what you should do).

Let say my application is myapp.py and it's working from step 1 to step N. I would make it return (simple Text), a number. This number is the its' step number. For example if I call http://localhost/myapp.py, it returns 5. After 30 minutes, I reload the page, it returns 15.

Now to the JavaScript thing. I will recommend using jQuery. It makes things a lot easier and faster. All I would do is

$('#streaming').load('http://localhost/myapp.py');

This will load the content of the page to the DIV with ID = streaming. But that will happen only when you load the page. To make that happen every x seconds, use JavaScript settimeout function.

var t=setTimeout("javascript statement",milliseconds);

It would be better to store the part that load the step in a function and call it when the page load and with setTimeout. It would help also to stop setTimeout once your script stops working (with a special number/message).


Using plain JavaScript, your choices are Comet, WebSockets, and Ajax. I don't really know much about any of those, however. Check out some of these websites:

http://en.wikipedia.org/wiki/Comet_(programming)

http://www.websockets.org/

From what I know, Comet simply involves making a long-lived Ajax request to the web server, where a script can push data as necessary. WebSockets are only supported in the very latest versions of some browsers. The simplest solution would be what Omar Abid suggested: poll the web server every once in a while via Ajax.


If you just what a quick solution rather than a full web page, check out SimpleHTTPServer You can write your state information in the form of a file to a directory and it and it is updated in for others to see.

Not beautiful, but very quick and very easy.

For a demo, just type python -m SimpleHTTPServer This will create a web page with the address of http://[your computer's name or IP]:8000 with the contents of the current working directory it is in when started. The files are hot linked. If you click on a file, it is displayed in the browser; if you click on a directory, you change to that directory and its contents are listed.

For your use, you could start with an empty directory and create files that match the steps you want to report. You could have files named your steps, "step 1.txt" for example, and more detailed info inside that file. Step 1 could be a directory that your script creates when step 1 is complete, etc.

Alternatively, check out Twisted, which will allow you to serve HTTP requests from your Python script directly.

If / when you want to get fancier, you can use essentially the same method. From your python script you write the output into files in an empty directory. From a web server with PHP, write a trivial script that checks that directory and displays what was found. Set the refresh interval on the page to some value and it will self update in the viewer's browsers.


You could add a server instance to your application that responds to queries on a given port with whatever your application decides to share. Kick this off in a separate thread with access to the applications status information then have your web page or monitoring programs send a requests to that port. You can, of course, respond to different requests in different ways. All the information you need is on https://pymotw.com/3/socket/tcp.html but the code below will get you started:

# borrowed from https://pymotw.com/3/socket/tcp.html
import socket
import sys

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to the port
# (the host name used will need to be pingable from the requesting service)
server_address = ('', 10000)
print('starting up on {} port {}'.format(*server_address))
sock.bind(server_address)

# Listen for incoming connections
sock.listen(1)

while True:  # add a terminator based on your application state
    # Wait for a connection
    print('waiting for a connection')
    connection, client_address = sock.accept()
    try:
        print('connection from', client_address)

        # Receive the data in small chunks and retransmit it
        while True:
            data = connection.recv(16)
            # print('received {!r}'.format(data))
            if data:  # add a check to ensure the request is valid
                print('sending data back to the client')
                data = 'Current application state is:' + getStatus()
                connection.sendall(data.encode())
            else:
                print('no data from', client_address)
                break
    finally:
        # Clean up the connection
        connection.close()
0

精彩评论

暂无评论...
验证码 换一张
取 消