python SSDP discovery error - windows

req = ['M-SEARCH * HTTP/1.1',
'HOST: 239.255.255.250:1900',
'MAN: "ssdp:discover"',
'ST: ssdp:all',
'MX: 3',
"", ""]
req = '\r\n'.join(req)
sock = socket(AF_INET, SOCK_DGRAM)
[sock.sendto(req, ('239.255.255.250',1900)) for i in range(3)]
resp, (addr,port) = sock.recvfrom(1024)
the SSDP discovery request i perform with this code retrieves me only the rootdevice instead of all the LAN connected devices (samsung tv, sky+hd box, laptop, PCs).
does anyone know how to display all the devices?

You only wait for one answer so it seems logical that you only get one :)
This should show all of them (including the duplicates that the devices/services send).
while (True):
resp, (addr,port) = sock.recvfrom(1024)
print resp

Related

How to get IP and Port from net.Addr when it could be a net.UDPAddr or net.TCPAddr

I'm running a hybrid server which listens on both TCP and UDP and need to get the local port, remote IP address and remote port. Currently the way I'm checking if the underlying type is net.UDPAddr or a net.TCPAddr is the following:
// BAD: but not sure a better way
switch reflect.TypeOf(remoteAddr).String() {
case "*net.UDPAddr":
p.SrcIP = remoteAddr.(*net.UDPAddr).IP.String()
p.SrcPort = uint(remoteAddr.(*net.UDPAddr).Port)
p.DstPort = uint(localAddr.(*net.UDPAddr).Port)
case "*net.TCPAddr":
p.SrcIP = remoteAddr.(*net.TCPAddr).IP.String()
p.SrcPort = uint(remoteAddr.(*net.TCPAddr).Port)
p.DstPort = uint(localAddr.(*net.TCPAddr).Port)
}
I'm not the greatest fan of this, if anyone has any cleaner looking solutions that would be greatly appreciated
No need for reflection, just do a proper type assertion switch instead:
switch addr := remoteAddr.(type) {
case *net.UDPAddr:
p.SrcIP = addr.IP.String()
p.SrcPort = uint(addr.Port)
p.DstPort = uint(localAddr.(*net.UDPAddr).Port)
case *net.TCPAddr:
p.SrcIP = addr.IP.String()
p.SrcPort = uint(addr.Port)
p.DstPort = uint(localAddr.(*net.TCPAddr).Port)
}

gammu phones tables with multiple modem port

EDIT: I know, after some research, this problem caused by IMEI field
in phones tables as primary, if we using modem pool like wavecome with
16 port, gammu detect just one IMEI
i have 1 modem connected with 16 port of sim card,each config connected to same database on my server,send and receive sms all working like a charm, each port have smsd services, like
gammu-smsd -c /etc/gammu-smsdrc-modem1 --pid /var/run/gammu-smsdrc-modem1 --daemon
gammu-smsd -c /etc/gammu-smsdrc-modem2 --pid /var/run/gammu-smsdrc-modem2 --daemon
each port have their own PhoneID, like modem1 and modem2, the problem is
why phones tables in gammu databases keep replacing the data with last gammu-smsd services run ?
ex:
if i run the first config, then phones tables will contains all informations , like signal, IMEI from 1st port, but when i run 2nd gammu-smsd data from 1st port will gone, changed from 2nd port config
here is my smsdrc config from modem1 /etc/gammu-smsdrc-modem1
[gammu]
port = /dev/ttyUSB0
model =
connection = at115200
synchronizetime = yes
logfile = /var/log/gammu-smsdrc-modem1
logformat = nothing
use_locking =
gammuloc =
[smsd]
service=sql
logfile=/var/log/gammu-smsdrc-modem1
debuglevel=0
Driver=native_mysql
User=root
Password=root
PC=localhost
Database=test
PhoneID=modem1
here is my smsd config from modem2 /etc/gammu-smsdrc-modem2
[gammu]
port = /dev/ttyUSB1
model =
connection = at115200
synchronizetime = yes
logfile = /var/log/gammu-smsdrc-modem2
logformat = nothing
use_locking =
gammuloc =
[smsd]
service=sql
logfile=/var/log/gammu-smsdrc-modem2
debuglevel=0
Driver=native_mysql
User=root
Password=root
PC=localhost
Database=test
PhoneID=modem2
after some reading on API Doc of gammu, i have figure it out, yes like the first one, it because i use one modem with 16 port of sim card, gammu just detect singel IMEI even the modem have 16 port, quick answer for my question is no configureable file can handle that problem, so we have to modify some line og code from smsd/services/sql.c
if (SMSDSQL_option(Config, SQL_QUERY_DELETE_PHONE, "delete_phone",
"DELETE FROM phones WHERE ", ESCAPE_FIELD("IMEI"), " = %I", NULL) != ERR_NONE) {
return ERR_UNKNOWN;
}
.......
.......
.......
if (SMSDSQL_option(Config, SQL_QUERY_UPDATE_RECEIVED, "update_received",
"UPDATE phones SET ",
ESCAPE_FIELD("Received"), " = ", ESCAPE_FIELD("Received"), " + 1"
" WHERE ", ESCAPE_FIELD("IMEI"), " = %I", NULL) != ERR_NONE) {
return ERR_UNKNOWN;
}
the final code will be
if (SMSDSQL_option(Config, SQL_QUERY_DELETE_PHONE, "delete_phone",
"DELETE FROM phones WHERE ", ESCAPE_FIELD("ID"), " = %P", NULL) != ERR_NONE) {
return ERR_UNKNOWN;
}
.......
.......
.......
if (SMSDSQL_option(Config, SQL_QUERY_UPDATE_RECEIVED, "update_received",
"UPDATE phones SET ",
ESCAPE_FIELD("Received"), " = ", ESCAPE_FIELD("Received"), " + 1"
" WHERE ", ESCAPE_FIELD("ID"), " = %P", NULL) != ERR_NONE) {
return ERR_UNKNOWN;
}
and recompile gammu as usual, and modify phones tables to set ID as Primary key, i'm not expert in c, hope some one can made a good change for better result, but for me it's enough for temp used.

How to implement Mac OS HTTP monitor

I need write application that monitoring all http requests/responces on mac os. How to register proxy server in Mac OS.
I found simple example - python tcp server that redirect requests/responses and code that automation set Mac OS proxies settings.
Python script was taken from http://www.cppfun.com/python-2-7-simple-http-proxy-server.htm, Mac OS proxies settings configurator from How to set proxy settings on MacOS using python
import socket, sys
import os
from thread import *
max_conn = 8
buffer_size = 8192
def proxy_on(port):
os.system('networksetup -setwebproxy Ethernet '+'127.0.0.1'+' '+str(port))
def proxy_off():
os.system('networksetup -setwebproxystate Ethernet off')
def app():
try:
listen_port = int(raw_input("[*] Enter listening port(a number eg 8098):"))
proxy_on(listen_port)
start(listen_port)
except KeyboardInterrupt:
print "\n[*] User requested interrupt\n[*] Program exiting ..."
sys.exit()
def start(listen_port):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', listen_port))
s.listen(max_conn)
print "[*] Init sockets ... Done"
print "[*] Sockets bind success ..."
print "[*] Proxy server success [%d]\n" % listen_port
except Exception, e:
print "\n", e
print "[*] Unable to init socket, maybe try another port"
sys.exit(2)
while True:
try:
conn, addr = s.accept()
data = conn.recv(buffer_size)
start_new_thread(conn_str, (conn, data, addr))
except KeyboardInterrupt:
s.close()
print "\n[*] Proxy server shutdown ..."
print "[*] Have a good day !"
proxy_off()
sys.exit(1)
s.close()
def conn_str(conn, data, addr):
try:
first_line = str(data).split('\n')[0]
url = first_line.split(' ')[1]
print url
host, port = get_host_and_port(url)
print host, port, data
proxy_server(host, port, conn, addr, data)
except Exception, e:
print "\n", e
print "[*] Get the http url or port fail ..."
sys.exit(1)
def get_host_and_port(url):
import urllib
_, rest = urllib.splittype(url)
host, rest = urllib.splithost(rest)
host, port = urllib.splitport(host)
if port is None:
port = 80
return host,port
def proxy_server(host, port, conn, addr, data):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
s.send(data)
while True:
reply = s.recv(buffer_size)
if len(reply) > 0:
conn.send(reply)
dat = float(len(reply))/1024.0
dat = "%.3s KB" % str(dat)
print "[*] Request done : %s => %s <=" % (addr[0], dat)
else:
break
s.close()
conn.close()
except socket.error, (value, message):
print "\n[*] Socket error %d:%s" % (value, message)
s.close()
conn.close()
sys.exit(1)
if __name__ == '__main__':
# power by cppfun.com
app()
# also you can change it yourself

Streaming audio to multiple AirPlay devices

Anyone know how to stream audio to multiple AirPlay destinations? Apparently, this was possible through Core Audio at some point in the past, but on 10.9 and 10.10, this does not seem possible. iTunes does it, so what's the secret? Here is some code I tried to see if I could get this to work:
OSStatus err = 0;
UInt32 size = sizeof(UInt32);
SSAudioSource * targetSource = airplayDevice.airplaySources[0];
AudioDeviceID airPlayDeviceID = targetSource.deviceID;
SSAudioSource * source1 = airplayDevice.airplaySources[0];
SSAudioSource * source2 = airplayDevice.airplaySources[1];
SSAudioSource * source3 = airplayDevice.airplaySources[2];
AudioDeviceID alldevices[] = {source3.sourceID, source2.sourceID, source1.sourceID};
AudioObjectPropertyAddress addr;
addr.mSelector = kAudioDevicePropertyDataSource;
addr.mScope = kAudioDevicePropertyScopeOutput;
addr.mElement = kAudioObjectPropertyElementMaster;
// Set the 'AirPlay' device to point to all of its sources...
err = AudioObjectSetPropertyData(airPlayDeviceID, &addr, 0, nil, size, alldevices);
AudioObjectPropertyAddress audioDevicesAddress = {
kAudioHardwarePropertyDefaultOutputDevice,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster
};
// ...now set the system output to point at the 'AirPlay' device
err = AudioObjectSetPropertyData(kAudioObjectSystemObject, &audioDevicesAddress, 0, nil, size, &airPlayDeviceID);
No matter how I arrange the devices in the array, sound only comes out of the first device (index 0) of the array. So what's the secret?
Thanks
I raised a bug report with Apple for this back in July and got a reply in October:
Engineering has determined that there are no plans to address this
issue.
I've gone back to Apple asking why the functionality has been removed but not hopeful for a (timely) response.
For what it's worth I think your approach is correct, it's similar to the way I had it working in the past for an app. I suspect iTunes uses Audio Units or something similar to do multiple speakers.

CoreMidi with MacRuby

I am trying to use the CoreMidi.framework with MacRuby and I am getting hung up on the correct way to implement the CoreMidi mechanisms in Ruby. I have created the MIDIClient and the OutputPort:
clientName = "Client"
clientRef = Pointer.new(:uint)
MIDIClientCreate( clientName, nil, nil, clientRef )
portName = "Output"
outport = Pointer.new(:uint)
MIDIOutputPortCreate( clientRef[0], portName, outport )
numberOfDestinations = MIDIGetNumberOfDestinations()
destination = MIDIGetDestination( 0 )
After this, I am at a loss for exactly what to do. I need to create a MIDIPacketList to send to MIDISend, but I'm not sure how this would be done in Ruby. The program hangs when I try to create a packet list with:
packetList = MIDIPacketList.new
Any suggestions?

Resources