Difference between revisions of "Bokeh: Menerima TCP data"

From OnnoWiki
Jump to navigation Jump to search
Line 1: Line 1:
 
Sumber: http://stackoverflow.com/questions/33241229/real-time-bokeh-socket-python/33275677#33275677
 
Sumber: http://stackoverflow.com/questions/33241229/real-time-bokeh-socket-python/33275677#33275677
  
client.py
+
==client.py==
  
 
  import sys
 
  import sys
Line 16: Line 16:
 
   
 
   
 
  try:
 
  try:
     for i in xrange(100):
+
     for i in xrange(100000):
 +
#    for i in xrange(10):
 
         m0 = random.random()
 
         m0 = random.random()
        m1 = random.random()
+
#        m1 = random.random()
        m2 = random.random()
+
#        m2 = random.random()
        m3 = random.random()
+
#        m3 = random.random()
        m4 = random.random()
+
#        m4 = random.random()
        m5 = random.random()
+
#        m5 = random.random()
        message = "%1.4f" % m1 + " %1.4f" % m2 + " %1.4f" % m3 + " %1.4f" % m4 + " %1.4f" % m5  + "\n"
+
#        message = "%1.4f" % m1 + " %1.4f" % m2 + " %1.4f" % m3 + " %1.4f" % m4 + " %1.4f" % m5  + "\n"
 +
        message = "%1.4f" % m0
 
         print >>sys.stderr, 'sending ' , message
 
         print >>sys.stderr, 'sending ' , message
 
         sock.sendall(message)
 
         sock.sendall(message)
         time.sleep(0.01)
+
         time.sleep(0.5)
 
   
 
   
 
  finally:
 
  finally:
Line 37: Line 39:
  
  
server.py
 
  
import sys
+
==server.py==
import time
+
 
import socket
+
import sys
from pylab import *
+
import time
from bokeh.plotting import cursession, figure, show, output_server
+
import socket
+
from bokeh.plotting import figure, output_server, show, cursession
output_server("raw_values")
+
 
+
output_server("raw",url='http://192.168.0.100:5006')
# Visualization workflow
+
 
counter = 0
+
# Visualization workflow
rx = [0]
+
counter = 0
ry = [0]
+
rx = [0]
rz = [0]
+
x = [0]
p = figure()
+
p = figure()
x = [0]
+
p.line(x, rx, name='raw')
p.line(x, rx, name='raw_mx')
+
 
+
# Create a TCP/IP socket
# Create a TCP/IP socket
+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+
 
+
# Bind the socket to the port
# Bind the socket to the port
+
server_address = ('192.168.0.100', 2000)
server_address = ('localhost', 2000)
+
print >>sys.stderr, 'starting up on %s port %s' % server_address
print >>sys.stderr, 'starting up on %s port %s' % server_address
+
sock.bind(server_address)
sock.bind(server_address)
+
 
+
# Listen for incoming connections
# Listen for incoming connections
+
sock.listen(1)
sock.listen(1)
+
 
+
show(p)
renderer = p.select(dict(name="raw_mx"))
+
 
ds = renderer[0].data_source
+
 
+
renderer = p.select(dict(name="raw"))
show(p)
+
ds = renderer[0].data_source
+
 
while True:
+
 
    # Wait for a connection
+
 
    print >>sys.stderr, 'waiting for a connection'
+
while True:
    connection, client_address = sock.accept()  
+
    # Wait for a connection
+
    print >>sys.stderr, 'waiting for a connection'
    try:
+
    connection, client_address = sock.accept()
        print >>sys.stderr, 'connection from', client_address  
+
 
+
    try:
        while True:
+
        print >>sys.stderr, 'connection from', client_address
            data = fromstring(connection.recv(24))
+
 
            print data  
+
        while True:
 +
            data = connection.recv(20)
 +
            print data
  
 
             if len(data) == 0:
 
             if len(data) == 0:
                break
+
              break
+
 
            rx += [data[0]]
+
            rx += [data]
            ry += [data[1]]
+
            x += [counter]
            rz += [data[2]]
+
            counter += 1
            x += [counter]
+
 
            counter += 1  
+
            ds.data["x"] = x
+
            ds.data["rmag_x"] = rx
            ds.data["x"] = x
+
            cursession().store_objects(ds)
            ds.data["rmag_x"] = rx
 
            cursession().store_objects(ds)
 
            time.sleep(0.01)
 
 
    finally:
 
        # Clean up the connection
 
        connection.close()
 
  
 +
            time.sleep(0.01)
  
 +
    finally:
 +
        # Clean up the connection
 +
        connection.close()
  
  

Revision as of 08:40, 28 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(100000):
#     for i in xrange(10):
        m0 = random.random()
#        m1 = random.random()
#        m2 = random.random()
#        m3 = random.random()
#        m4 = random.random()
#        m5 = random.random()
#        message = "%1.4f" % m1 + " %1.4f" % m2 + " %1.4f" % m3 + " %1.4f" % m4 + " %1.4f" % m5  + "\n"
        message = "%1.4f" % m0
        print >>sys.stderr, 'sending ' , message
        sock.sendall(message)
        time.sleep(0.5)

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




server.py

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

output_server("raw",url='http://192.168.0.100:5006')

  1. Visualization workflow

counter = 0 rx = [0] x = [0] p = figure() p.line(x, rx, name='raw')

  1. Create a TCP/IP socket

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

  1. Bind the socket to the port

server_address = ('192.168.0.100', 2000) print >>sys.stderr, 'starting up on %s port %s' % server_address sock.bind(server_address)

  1. Listen for incoming connections

sock.listen(1)

show(p)


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


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 = connection.recv(20)
           print data
           if len(data) == 0:
              break
           rx += [data]
           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