Difference between revisions of "Bokeh: Menerima TCP data"

From OnnoWiki
Jump to navigation Jump to search
(New page: Sumber: http://stackoverflow.com/questions/33241229/real-time-bokeh-socket-python/33275677#33275677 client.py import sys import time import socket from pylab import randn # Create...)
 
Line 6: Line 6:
 
  import time
 
  import time
 
  import socket
 
  import socket
  from pylab import randn
+
  import random
 
   
 
   
 
  # Create a TCP/IP socket
 
  # Create a TCP/IP socket
Line 12: Line 12:
 
   
 
   
 
  # Connect the socket to the port where the server is listening
 
  # Connect the socket to the port where the server is listening
  server_address = ('localhost', 2000)
+
  server_address = ('192.168.0.100', 2000)
 
  sock.connect(server_address)
 
  sock.connect(server_address)
 
   
 
   
 
  try:
 
  try:
 
     for i in xrange(100):
 
     for i in xrange(100):
         m = randn(3)
+
         m = random.random()
         message = m.tostring()
+
         message = "%1.4f" % m + "\n"
         print >>sys.stderr, 'sending "%s"' % m
+
         print >>sys.stderr, 'sending "%f"' % m
 
         sock.sendall(message)
 
         sock.sendall(message)
         time.sleep(0.01)  
+
         time.sleep(0.01)
 
   
 
   
 
  finally:
 
  finally:
 
     print >>sys.stderr, 'closing socket'
 
     print >>sys.stderr, 'closing socket'
 
     sock.close()
 
     sock.close()
 +
  
  

Revision as of 16:41, 27 November 2015

Sumber: http://stackoverflow.com/questions/33241229/real-time-bokeh-socket-python/33275677#33275677

client.py

import sys
import time
import socket
import random

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

# Connect the socket to the port where the server is listening
server_address = ('192.168.0.100', 2000)
sock.connect(server_address)

try:
    for i in xrange(100):
        m = random.random()
        message = "%1.4f" % m + "\n"
        print >>sys.stderr, 'sending "%f"' % m
        sock.sendall(message)
        time.sleep(0.01)

finally:
    print >>sys.stderr, 'closing socket'
    sock.close()


server.py

import sys
import time
import socket
from pylab import *
from bokeh.plotting import cursession, figure, show, output_server

output_server("raw_values")

# Visualization workflow
counter = 0
rx = [0]
ry = [0]
rz = [0]
p = figure()
x = [0]
p.line(x, rx, name='raw_mx')

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

# Bind the socket to the port
server_address = ('localhost', 2000)
print >>sys.stderr, 'starting up on %s port %s' % server_address
sock.bind(server_address)

# Listen for incoming connections
sock.listen(1)

renderer = p.select(dict(name="raw_mx"))
ds = renderer[0].data_source

show(p)

while True:
    # Wait for a connection
    print >>sys.stderr, 'waiting for a connection'
    connection, client_address = sock.accept() 

    try:
        print >>sys.stderr, 'connection from', client_address 

        while True:
            data = fromstring(connection.recv(24))
            print data 
           if len(data) == 0:
               break

            rx += [data[0]]
            ry += [data[1]]
            rz += [data[2]]
            x += [counter]
            counter += 1 

            ds.data["x"] = x
            ds.data["rmag_x"] = rx
            cursession().store_objects(ds)
            time.sleep(0.01)

    finally:
        # Clean up the connection
        connection.close()



Referensi