Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS 

Send custom UDP packets in Ruby


From: http://www.ruby-forum.com/topic/124159
Author: Bill Kelly

For yet another nifty UDP snippet see Skype-Style Firewall Busting with Ruby and UDP.



require 'socket'

#abort "Usage: server_addr, server_port, cmd_str" unless ARGV.length == 3

UDP_RECV_TIMEOUT = 3  # seconds

def q2cmd(server_addr, server_port, cmd_str)
  resp, sock = nil, nil
  begin
   cmd = "\377\377\377\377#{cmd_str}\0"
    sock = UDPSocket.open
    sock.send(cmd, 0, server_addr, server_port)
    resp = if select([sock], nil, nil, UDP_RECV_TIMEOUT)
      sock.recvfrom(65536)
    end
    if resp
      resp[0] = resp[0][4..-1]  # trim leading 0xffffffff
    end
  rescue IOError, SystemCallError
  ensure
    sock.close if sock
  end
  resp ? resp[0] : nil
end

# your firewall has to allow communication with IP address 67.19.248.74 (port 27912)
#server, port, cmd = *ARGV
server = "tastyspleen.net"
port = 27912
cmd = "status"

result = q2cmd(server, port, cmd)
puts result


Python - SendSMS over BT and AT

// Send SMS over Bluetooth (AT Command)

import bluetooth

sockfd = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sockfd.connect(('00:00:00:00:00:00', 1)) # BT Address
sockfd.send('ATZ\r')
sockfd.send('AT+CMGF=1\r')
sockfd.send('AT+CSCA="+393359609600"\r') # Client TIM ITA
sockfd.send('AT+CMGS="+39xxxxxxxxxx"\r') # TO PhoneNumber
sockfd.send('Messaggio da mandare...\n')
sockfd.send(chr(26)) # CTRL+Z
sockfd.close()

PyS60 - SendFile

// Send File over Bluetooth

from appuifw import *
from e32socket import *

try:
    phone = bt_obex_discover()
    file = query(u'File Selection', 'text')
    bt_obex_send_file(phone[0], phone[1].values()[0], file)
    note(u'File Sent')
except Exception, error:
    note(unicode(error), 'error')

Sending html mail

Get the code for createhtmlmail(html, text, subject) here.
import smtplib
html = open("newsletter.html").read()
text = open("newsletter.txt").read()
subject = "Today's Newsletter!"

message = createhtmlmail(html, text, subject)

server = smtplib.SMTP("localhost")
server.sendmail('my@dress.com', 'your@dress.com', message)
server.quit()

Send a file using FTP

import ftplib
s = ftplib.FTP('myserver.com','login','password') # Connect

f = open('todo.txt','rb')                # file to send
s.storbinary('STOR todo.txt', f)         # Send the file

f.close()                                # Close file and FTP
s.quit()

Taken (with some mod.) from here
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS