Vizible’s Blog

Python – Broadcast UDP

Posted in Python by wearetherock on January 31, 2009

broadcast_server.py , have to start tomboy

import socket
import dbus

def show_message(title, message):
    bus = dbus.SessionBus()
    object = bus.get_object("org.gnome.Tomboy", "/org/gnome/Tomboy/RemoteControl")
    tomboy = dbus.Interface(object, "org.gnome.Tomboy.RemoteControl")

    note = tomboy.CreateNamedNote(title)
    tomboy.SetNoteContents(note , message)
    tomboy.AddTagToNote(note,title)
    tomboy.DisplayNote(note)

def main():
    my_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    my_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    my_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
    my_socket.bind(('',8881))

    print 'start service ...'

    while True :
        message , address = my_socket.recvfrom(8192)
        print 'message (%s) from : %s' % ( str(message), address[0])
        show_message('message from :'+ str(address[0]) , message)

if __name__ == "__main__" :
    main()

|
broadcast_client.py

import socket
import sys

def main(message) :
	my_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
	my_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST,1)
	my_socket.sendto("wearetherock", ('<broadcast>' ,8881))
	my_socket.close()

if len(sys.argv) < 2 :
	print 'use: python tomboy_client.py "message"'
else :
	main(sys.argv[1]

|

Advertisements
Tagged with: , , , ,

7 Responses

Subscribe to comments with RSS.

  1. […] a comment » ทำต่อจากอันเดิม Python – Broadcast UDP เมื่อ Tomboy บันทึกข้อมูล(เป็นช่วง ๆ) […]

  2. BlackForestCowboy said, on November 18, 2009 at 9:24 am

    Hey, thanks for the hints, made it easy to use Broadcasts with python for me!

  3. Adam said, on April 29, 2012 at 9:56 pm

    Thanks, this is very helpful.

  4. t.mhahaha said, on August 23, 2012 at 1:49 am

    แจ่มมากครับ

  5. t.mhahaha said, on August 23, 2012 at 4:18 am

    มันมี error บอกว่าไม่มี attribute AttributeError: ‘module’ object has no attribute ‘SQL_SOCKET’ ต้องลง python-mysql อะไรมัยครับ

  6. Johne631 said, on May 7, 2014 at 8:01 am

    Every weekend i used to pay a visit this website, for the reason that i wish for enjoyment, for the reason cdadaebkeddd

  7. […] You can try broadcast UDP, I found some example here: https://vizible.wordpress.com/2009/01/31/python-broadcast-udp/ […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: