Simple sockets to talk to a scrolling display using python

I need to talk to a little box that has an ip address and accepts tcp input to send on to a giant LED display.  I’m going to send messages across it as if it were the stock market tickers streaming along, or like the rolling display in Times Square that reports the news.

What do I want to use to do this while keeping it simple and straight forward? Java? C++? hmm… no wait I know, Python!!!  It’s just so freaking easy to do in Python.  Python is quickly becoming my favorite language.  There’s so much support for it, its like you can do just about anything in Python now-a-days!

So here’s a basic little test application to send some messages just so I can make sure the communication works.

from socket import *

host = 10.1.2.3

port = 80

s = socket(AF_INET, SOCK_STREAM)

s.connect((host, port))

s.send(”TEST123\n”)

That’s about as simple as it can get.  This won’t get us very much, but it’s a start and it lets me know I am talking to the right host, and that it’s accepting my connection.  If there was some sort of ACL on the little black box or it was listening for UDP, i’d get an immediate “Connection Refused” message when I executed this code and it got to the s.connect line.

I could start here and later build a nice app that makes letters and words scroll by.  Maybe I’ll even connect multiple displays and locate them physically next to each other.  Then I could have a message scroll through one display and then through the next, and the next.  But i’m getting ahead of myself here.

class mysocket:
        def __init__(self, sock=None):
            if sock is None:
                self.sock = socket.socket(
                    socket.AF_INET, socket.SOCK_STREAM)
            else:
                self.sock = sock
        def connect(host, port):
            self.sock.connect((host, port))
        def mysend(msg):
            totalsent = 0
            while totalsent < MSGLEN:
                sent = self.sock.send(msg[totalsent:])
                if sent == 0:
                    raise RuntimeError, "socket connection broken"
                totalsent = totalsent + sent
        def myreceive():
            msg = ''
            while len(msg) < MSGLEN:
                chunk = self.sock.recv(MSGLEN-len(msg))
                if chunk == '':
                    raise RuntimeError, "socket connection broken"
                msg = msg + chunk
            return msg
Posted by admica   @   7 November 2008
Tags : ,

Related Posts

Like this post? Share it!

Digg Twitter StumbleUpon Delicious Technorati Facebook RSS

0 Comments

No comments yet. Be the first to leave a comment !
Leave a Comment

Name

Email

Website

Previous Post
« Installing OpenSSL, OpenSSH, and RSYNC on Solaris 2.6 (SunOS)
Next Post
OpenLDAP + Replica + StartTLS encryption on Fedora in 10 minutes »
Powered by Wordpress   |   Lunated designed by ZenVerse