CSV Reading/Writing

We have recently finished coding the Python (used for writing to the CSV files) and jQuery (used to read them), and the code is available on GitHub. However, I include it here for convenience:

Outlets:

import socket
import sys

message = raw_input("Enter your message:") # Read message to be sent

# Create a UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = ('192.168.0.18', 6401) # Server's IP Adress and Port Number for Communication.

try:

    # Send data
    print (sys.stderr, 'sending "%s"' % message) # Print the message to be sent.
    sent = sock.sendto(message.encode('utf-8'), server_address) # Send the message

    # Receive response
    print (sys.stderr, 'waiting to receive') # Listen for acknowlegement of file receival 
    data, server = sock.recvfrom(4096) # Receive acknowledgement data
    print (sys.stderr, 'received "%s"' % data) #Print acknowledgement data

finally:
    print (sys.stderr, 'closing socket') # Print prior to closure of socket
    sock.close() # Close the socket

Hub - Python:

import socket
import sys

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

# Bind the socket to the port
server_address = ("192.168.0.21", 6401) #Server's IP Adress and Port Number for Communication.
print (sys.stderr, 'starting up on %s port %s' % server_address)
sock.bind(server_address)

while True:
    print (sys.stderr, '\nwaiting to receive message')
    data, address = sock.recvfrom(4096)
    print (sys.stderr, 'received %s bytes from %s' % (len(data), address))
    print (sys.stderr, data)

    if data:
        sent = sock.sendto("recieved data".encode('utf-8'), address)
    print (sys.stderr, 'sent %s bytes back to %s' % (sent, address))

Hub Website - jQuery:

$.get(csv_filepath, function(fileContent) {
    // run when data is loaded
}

To preview how the site would look in your home, please see our demo site; all of our code is available open-source on GitHub.

Comments

Popular posts from this blog

AI Tips

Project Overview

Introduction