I am interested in sending a TUIO bundle over a network and receiving it on the Unity side. Unity won't recognize the bundle, so I am assuming something is wrong with it.
(while sent every .1 seconds)
send_address = 'xxx.xxx.xxx.x', 3333
# OSC basic client
c = OSC.OSCClient()
c.connect( send_address ) # set the address for all following messages
bundle = OSC.OSCBundle()
bundle.append( {'addr':"/tuio/2Dcur", 'args':["source:", "Py#Unity.com"]} ) # and some more stuff ...
bundle.append( {'addr':"/tuio/2Dcur", 'args':["alive:", 21]} ) # and some more stuff ...
bundle.append( {'addr':"/tuio/2Dcur", 'args':["set:", 21, 0.328125, 0.6529948115348816, 0, 0, -2.5432088375091553]} ) # and some more stuff ...
bundle.setAddress("/tuio/2Dcur")
c.send(bundle) # send it!
Related
I have a server running on a desktop machine with IP address 192.168.1.11, and client code is running on server accessing through OpenVPN connect. When I run the below code client sends the request but server doesn't receives it.
Server.py:
context=zmq.Context()
socket=context.socket(zmq.REP)
socket.bind("tcp://*:8080")
while True:
message=socket.recv_pyobj()
print("%s:%s" %(message.get(1)[0],message.get(1)[1]))
socket.send_pyobj({1:[message.get(1)[0],message.get(1)[1]]})
Client.py
socket=context.socket(zmq.REQ)
socket.connect("tcp://192.168.1.11:8080")
name="Test"
while True:
message=input("Test Message")
socket.send_pyobj(({1:[name,message]}))
Thanks help is highly appreciated.
Q : "Issue in connecting client socket with server socket"
Step 0 : proof there has been achieved an OSI-ISO-Layer-3 visibility traceroute <targetIP>
Step 1 : having achieved a visible route to <targetIP>, repair the code to meet documented REQ/REP properties
Step 2 : having achieved a visible route to <targetIP> and REQ/REP, we should improve robustness of the code
context = zmq.Context()
socket = context.socket( zmq.REP )
socket.bind( "tcp://*:8080" )
#---------------------------------------------- # ROBUSTNESS CONFIGs
socket.setsockopt( zmq.LINGER, 0 ) # .set explicitly
socket.setsockopt( zmq.MAXMSGSIZE, ... ) # .set safety ceiling
socket.setsockopt( ..., ... ) # .set ...
#---------------------------------------------- # ROBUSTNESS CONFIGs
while True:
message = socket.recv_pyobj() # .recv() a request from REQ-side
print( "%s:%s" % ( message.get(1)[0], # shall improve robustness
message.get(1)[1] # for cases other than this
)
)
socket.send_pyobj( { 1: [ message.get(1)[0], # REP must "answer" to REQ
message.get(1)[1]
]
}
)
TARGET_IP = "<targetIP>" # <targetIP> from Step 0
PORT_NUMBER = 8080
socket = context.socket( zmq.REQ )
socket.connect( "tcp://{0:}:{1:}".format( TARGET_IP, PORT_NUMBER ) )
#---------------------------------------------- # ROBUSTNESS CONFIGs
socket.setsockopt( zmq.LINGER, 0 ) # .set explicitly
socket.setsockopt( zmq.MAXMSGSIZE, ... ) # .set safety ceiling
socket.setsockopt( ..., ... ) # .set ...
#---------------------------------------------- # ROBUSTNESS CONFIGs
name = "Test"
while True:
message = input( "Test Message" )
socket.send_pyobj( ( { 1: [ name, # REQ-side sends a request
message # here
] # bearing a tuple
} # with a dict
) # having a list
) # for a single key
#------------------------------------------ # REQ-side now MUST also .recv()
_ = socket.recv() # before it can .send() again
I have this code for my server:
import time
import zmq
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5000")
while True:
message = socket.recv()
socket.send(b"World")
print "sent"
while True:
print "done."
I have a separate client script that sends a message through zmq to this one whenever i send a message. On the server (this code), if i only have the first while True:, it prints "sent" every time i send a message, and if i only have the second while True:, it prints "done." continuously. But if i put both, it never prints done (or if i switch their order and put both it never prints "sent" when i send a message").
As an output i want it to continuously print "done.", and also print "sent" when I get a message. So something like this:
done.
done.
done.
done.
done.
sent
done.
lots more done....
Basically i want both loops to run continuously and completely independently of each other.
N.B. I have tried using multiprocessing (such as in the 3rd answer here How do I run two python loops concurrently?), but couldn't get that to work either. I tried it as below:
import time
import zmq
from multiprocessing import Process
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5000")
i = time.time()
def zmq_loop():
while True:
message = socket.recv()
socket.send(b"World")
print "sent"
def done_loop():
global i
while True:
i2 = time.time()-i
if i2%2 == 0:
print "done."
if __name__ == "__main__":
Process(target=zmq_loop).start()
Process(target=done_loop).start()
As was explained yesterday in this, the [CONCURRENT]-processing is technically achievable in several different ways in python, each with a way different cost.
Today, let's have a look onto another approach - using a framework, that was developed with the very same motivation - having the natural [CONCURRENT]-scheduling-already-in-DNA - originally intended for easy composing and smooth operating complex GUI Man-Machine-Interactions ( MMI ).
This framework may and will help you achieve a lot, right due to the fact, it has evolved with a lot of care for exactly the same scenarios, where more than one thing has to be monitored at once:
Welcome to Tkinter GUI framework, which we will use just for its smart concurrently operated event handlers.
I was many times positively surprised, how easy it comes to build a quite complex composition of Finite-State-Automata ( FSA ), that smoothly cooperate together ( a coalition of FSA-s ), using tools for both independent, isolated operations ( the inner-logic of each FSA ), yet being easily able to propagate signals / messages from one FSA towards another(s). Yes, they can actually operate in 1-event-source-FSA : N-consumer(s)-FSA(s)
There you can create ( with ZeroMQ always in a non-blocking manner ) handlers -- one "sniffer" for a regular checking ( best by a timeout-controlled .poll() method to { NACK | POSACK } anything to read ) -- another "reader" for actual reading from the ZeroMQ Socket() instance ( triggered by the POSACK-signal from the "sniffer", as was mentioned previously -- another "do-a-work-er" for any other task one may wish to operate
Tkinter .mainloop() method is the global controller, which orchestrates the dirty job for you.
The hich level concept of Tkinter-mediated agent's co-processing starts in main() as simple as:
def main():
root = Tk() # INIT a Tk() instance
root.lift() # + make it visible
app = myApplication( root ) # SET ( [HERE] are all your app gems )
root.mainloop() # START the core event-handling orchestrator
Tkinter might look as a garrage full of various GUI-gadgets, that have nothing to do with your problem, but don't panic.
Tkinter has incredibly well created tools right for your needs.
using control variables that will be used as a means for storing, signalling and propagating changes of values among otherwise independent and explicitly un-coordinated actors ( ref. "sniffer", "reader", "worker" and any others ... )
tools for handling events - real, abstract and even virtual
tools for handling timed-operations - in a form of an almost a lightweight real-time system, using setups with preferred timing of what shall happen next, the .mainloop()-yet tasked to bear in mind
an explicitly specified timing .after( thisAmountOfMILLISECONDS, callThisFUNCTION ) or a liberal .after_idle( callAlwaysThatFUNCTION ).
One does not need indeed anything more to go and solve your task, using these already perfect tools.
So all the rest is just in principle under your creativity how to re-use these smart tools.
A small demo,how to make two ( 3 ! ) things happen "at the same time independently"
Let's setup the case, when one wants to process ( here demonstrated by a printing ) several independent processes, all at the same time.
>>> #-----------------------------------------------FAST MOCK-UP EXAMPLE
>>> import Tkinter as tk # python27
>>> root = tk.Tk()
>>> root.protocol( "WM_DELETE_WINDOW", root.quit() )
'3071841620Ldestroy'
>>> #------VAR-------------------------------------IMPORTANT TOOL:
>>> aStringVAR = tk.StringVar()
>>> aStringVAR.set( "_init_" )
>>> def aKeyPressEventHANDLER( anEvent ): # SIMPLE EventHANDLER,
# # also ignites remote responsive processes
... aTemplate = "[KEY]::{3: >10s}\n<s/n>::{0: >10d}\n(=#=)::{1: > 10d}\n^from::({5:})"
... sString = aTemplate.format( anEvent.serial,
... anEvent.time,
... anEvent.char,
... anEvent.keysym,
... anEvent.keysym_num,
... str(anEvent.widget )
... )
... aStringVAR.set( sString )
... print sString
...
>>> #----VAR_TRACER----------------------------------------[#1]
>>> def aVAR_TRACER_A( p1_quasiNAME, p2_indexOrEmptyString, p3_accessMODE ):
... print "aVAR_TRACER_A()-called::(on){0:} traced_event({1:})".format( str( p1_quasiNAME ), str( p3_accessMODE ) )
... # ###############=[A]#######
... # < do some task =[A] here >
... # ###############=[A]#######
... print "aVAR_TRACER_A() [{0:}]".format( str( root.globalgetvar( p1_quasiNAME ) ).replace( " ", "" ) )
...
>>> #----VAR_TRACER----------------------------------------[#2]
>>> def aVAR_TRACER_B( p1_quasiNAME, p2_indexOrEmptyString, p3_accessMODE ):
... print "aVAR_TRACER_B()-called::(on){0:} traced_event({1:})".format( str( p1_quasiNAME ), str( p3_accessMODE ) )
... # ###############=[B]#######
... # < do some task =[B] here >
... # ###############=[B]######
... print "aVAR_TRACER_B() [{0:}]".format( str( root.globalgetvar( p1_quasiNAME ) ).replace( " ", "" ) )
...
>>> #-----VAR_A_tracer_ID------------------------------"w" EVENT SNIFFER
>>> aTraceVAR_A_tracer_ID = aStringVAR.trace_variable( "w", aVAR_TRACER_A )
>>> #-----VAR_B_tracer_ID------------------------------"w" EVENT SNIFFER
>>> aTraceVAR_B_tracer_ID = aStringVAR.trace_variable( "w", aVAR_TRACER_B )
>>> #-----------tracer_ID values for ev. theirs resp. de-activation:
>>> aTraceVAR_A_tracer_ID
'3071960124LaVAR_TRACER_A'
>>> aTraceVAR_B_tracer_ID
'3071961284LaVAR_TRACER_B'
>>> #---.bind()-----------------------EventHANDLER with a system event <KeyPress>
>>> root.bind( "<KeyPress>", aKeyPressEventHANDLER ) # <-since here LIVE (!)
'3071841740LaKeyPressEventHANDLER'
>>> #------------------------------------------------^^^ since here, it went live
>>> # 1: having put a mouse on tk-window,
>>> # 2: set-focus by click
>>> # 3: started keys:
>>> # ( "a",
>>> # 6-on-<NumKeyPad>,
>>> # *-on-<NumKeyPad>
>>> # this happened "independently, at the same time" ( see time (=#=):: values )
>>>
aVAR_TRACER_B()-called::(on)PY_VAR0 traced_event(w)
aVAR_TRACER_B() [[KEY]::a<s/n>::832(=#=)::88486992^from::(.)]
aVAR_TRACER_A()-called::(on)PY_VAR0 traced_event(w)
aVAR_TRACER_A() [[KEY]::a<s/n>::832(=#=)::88486992^from::(.)]
[KEY]:: a
<s/n>:: 832
(=#=):: 88486992
^from::(.)
aVAR_TRACER_B()-called::(on)PY_VAR0 traced_event(w)
aVAR_TRACER_B() [[KEY]::KP_6<s/n>::832(=#=)::88509107^from::(.)]
aVAR_TRACER_A()-called::(on)PY_VAR0 traced_event(w)
aVAR_TRACER_A() [[KEY]::KP_6<s/n>::832(=#=)::88509107^from::(.)]
[KEY]:: KP_6
<s/n>:: 832
(=#=):: 88509107
^from::(.)
aVAR_TRACER_B()-called::(on)PY_VAR0 traced_event(w)
aVAR_TRACER_B() [[KEY]::KP_Multiply<s/n>::832(=#=)::88541180^from::(.)]
aVAR_TRACER_A()-called::(on)PY_VAR0 traced_event(w)
aVAR_TRACER_A() [[KEY]::KP_Multiply<s/n>::832(=#=)::88541180^from::(.)]
[KEY]::KP_Multiply
<s/n>:: 832
(=#=):: 88541180
^from::(.)
I am trying to measure the responses back from DNS servers. Making a sniffer for a typical DNS response that is less than 512 bytes is no big deal. My issue is receiving large 3000+ byte responses - in some cases 5000+ bytes. I haven't been able to get a socket working that can receive that data reliably. Is there a way with Python sockets to receive from a specific source address?
Here is what I have so far:
import socket
import struct
def craft_dns(Qdns):
iden = struct.pack('!H', randint(0, 65535))
QR_thru_RD = chr(int('00000001', 2)) # '\x01'
RA_thru_RCode = chr(int('00100000', 2)) # '\x00'
Qcount = '\x00\x01' # question count is 1
ANcount = '\x00\x00'
NScount = '\x00\x00'
ARcount = '\x00\x01' # additional resource count is 1
pad = '\x00' #
Rtype_ANY = '\x00\xff' # Request ANY record
PROtype = '\x00\x01' # Protocol IN || '\x00\xff' # Protocol ANY
DNSsec_do = chr(int('10000000', 2)) # flips DNSsec bit to enable
edns0 = '\x00\x00\x29\x10\x00\x00\x00\x00\x00\x00\x00' # DNSsec disabled
domain = Qdns.split('.')
quest = ''
for x in domain:
quest += struct.pack('!B', len(x)) + x
packet = (iden+QR_thru_RD+RA_thru_RCode+Qcount+ANcount+NScount+ARcount+
quest+pad+Rtype_ANY+PROtype+edns0) # remove pad if asking <root>
return packet
def craft_ip(target, resolv):
ip_ver_len = int('01000101', 2) # IPvers: 4, 0100 | IP_hdr len: 5, 0101 = 69
ipvers = 4
ip_tos = 0
ip_len = 0 # socket will put in the right length
iden = randint(0, 65535)
ip_frag = 0 # off
ttl = 255
ip_proto = socket.IPPROTO_UDP # dns, brah
chksm = 0 # socket will do the checksum
s_addr = socket.inet_aton(target)
d_addr = socket.inet_aton(resolv)
ip_hdr = struct.pack('!BBHHHBBH4s4s', ip_ver_len, ip_tos, ip_len, iden,
ip_frag, ttl, ip_proto, chksm, s_addr, d_addr)
return ip_hdr
def craft_udp(sport, dest_port, packet):
#sport = randint(0, 65535) # not recommended to do a random port generation
udp_len = 8 + len(packet) # calculate length of UDP frame in bytes.
chksm = 0 # socket fills in
udp_hdr = struct.pack('!HHHH', sport, dest_port, udp_len, chksm)
return udp_hdr
def get_len(resolv, domain):
target = "10.0.0.3"
d_port = 53
s_port = 5353
ip_hdr = craft_ip(target, resolv)
dns_payload = craft_dns(domain) # '\x00' for root
udp_hdr = craft_udp(s_port, d_port, dns_payload)
packet = ip_hdr + udp_hdr + dns_payload
buf = bytearray("-" * 60000)
recvSock = socket.socket(socket.PF_PACKET, socket.SOCK_RAW, socket.ntohs(0x0800))
recvSock.settimeout(1)
sendSock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
sendSock.settimeout(1)
sendSock.connect((resolv, d_port))
sendSock.send(packet)
msglen = 0
while True:
try:
pkt = recvSock.recvfrom(65535)
msglen += len(pkt[0])
print repr(pkt[0])
except socket.timeout as e:
break
sendSock.close()
recvSock.close()
return msglen
result = get_len('75.75.75.75', 'isc.org')
print result
For some reason doing
pkt = sendSock.recvfrom(65535)
Recieves nothing at all. Since I'm using SOCK_RAW the above code is less than ideal, but it works - sort of. If the socket is extremely noisy (like on a WLAN), I could end up receiving well beyond the DNS packets, because I have no way to know when to stop receiving packets when receiving a multipacket DNS answer. For a quiet network, like a lab VM, it works.
Is there a better way to use a receiving socket in this case?
Obviously from the code, I'm not that strong with Python sockets.
I have to send with SOCK_RAW because I am constructing the packet in a raw format. If I use SOCK_DGRAM the custom packet will be malformed when sending to a DNS resolver.
The only way I could see is to use the raw sockets receiver (recvSock.recv or recvfrom) and unpack each packet, look if the source and dest address match within what is supplied in get_len(), then look to see if the fragment bit is flipped. Then record the byte length of each packet with len(). I'd rather not do that. It just seems there is a better way.
Ok I was stupid and didn't look at the protocol for the receiving socket. Socket gets kind of flaky when you try to receive packets on a IPPROTO_RAW protocol, so we do need two sockets. By changing to IPPROTO_UDP and then binding it, the socket was able to follow the complete DNS response over multiple requests. I got rid of the try/catch and the while loop, as it was no longer necessary and I'm able to pull the response length with this block:
recvSock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_UDP)
recvSock.settimeout(.3)
recvSock.bind((target, s_port))
sendSock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
#sendSock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sendSock.settimeout(.3)
sendSock.bind((target, s_port))
sendSock.connect((resolv, d_port))
sendSock.send(packet)
pkt = recvSock.recvfrom(65535)
msglen = len(pkt[0])
Now the method will return the exact bytes received from a DNS query. I'll leave this up in case anyone else needs to do something similar :)
I've tried so many ways the past week or so to try to get this to work.
I've managed to get Skype2IRC to work in another script, and not spam IRC or anything. It was good. Just on message status, send message to IRC. That part I can get down easily. However, the part going from IRC to Skype is where I'm having issues. First, I figured I'd try to get it to work with multi-threading. Didn't go out well. Then, I tried this;
import os,sys,time,Skype4Py,socket,re,string
from random import choice
s = Skype4Py.Skype()
s.Attach() #Attach to Skype. Make sure to accept permissions from Skype client!
name = "Ibex"
network = "irc.myserver.net"
channel = "#Skype"
port = 6667
irc = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
irc.connect ( ( network, port ) )
print irc.recv ( 4096 )
irc.send ( 'NICK '+name+'\r\n' )
irc.send ( 'USER '+name+' '+name+' '+name+' : IRC Bot\r\n' )
irc.send ( 'JOIN '+channel+'\r\n' )
ircusername = '' #referencing variable so it can be used before, data will change in while loop
ircmessage = '' #same as above
def Ibex(Message, Status):
chat = Message.Chat
members = chat.MemberObjects
msg = Message.Body
send = Message.Chat.SendMessage
sAlias = Message.FromDisplayName
sUsername = Message.FromHandle
if Status == 'RECEIVED':
irc.send ( 'PRIVMSG '+channel+' :<Skype - '+sUsername+'>: '+msg+'\r\n' )
if ircusername != '':
try:
ircmessage = data.split(channel+" :",1)[1]
send("<IRC - "+ircusername+">: "+ircmessage)
except:
print "Error getting new IRC message."
s.OnMessageStatus = Ibex
while True:
data = irc.recv ( 4096 )
print data
if data.find ( 'PING' ) != -1:
irc.send ( 'PONG ' + data.split() [ 1 ] + '\r\n' )
if data.find ( 'KICK' ) != -1:
irc.send ( 'JOIN '+channel+'\r\n' )
try:
ircusername = re.search(':(.*)!', data)
ircusername = ircusername.group(1)
except:
pass
After tons of trial and error. And, a LOT of spam in IRC and Skype, I managed to get this to send IRC messages to skype.
HOWEVER, it spams.
I'll send a message in IRC, and nothing will happen.
I'll send a message in Skype (hence s.OnMessageStatus = Ibex) and it sends the IRC message, as expected. However, it won't stop sending it. It spams it like crazy. I tried doing something like setting a sendMSG = True when a message is received, and setting sendMSG to false after sending it, then if sendMSG = true, then send. That didn't seem to work out well.
Anyone have any suggestions? Am I just missing something small causing this error? Thanks to anyone in advanced, this has been bothering me for a while now.
I managed to get an old IRC/Skype bridge to work a while ago, but that script is long gone and I can't remember exactly how I did it.
Well the bot I made in python and it is connecting to random servers. I cant figure out why. so far this code is mishmash of other projects so maybe I'm overlooking something. basicly I want it to connect to irc.rizon.net join #brook_nise then lurk there.
I conect to these servers when I run my script:
irc.rizon.io
irc.sxci.net
irc.broke-it.com
irc.rizon.sexy
.
import socket
network = 'irc.rizon.net'
network = network.encode(encoding='UTF-8',errors='strict')
port = 6667
irc = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
USR = "USER boxxxy boxxxy boxxxy :boxxxy\r\n"
PAS = '/msg NickServ IDENTIFY pass\r\n'
JOI = 'JOIN #brook_nise\r\n'
pi = 'PING'
po = 'PONG'
PING = pi.encode(encoding='UTF-8',errors='strict')
PONG = po.encode(encoding='UTF-8',errors='strict')
USER = USR.encode(encoding='UTF-8',errors='strict')
PASS = PAS.encode(encoding='UTF-8',errors='strict')
JOIN = JOI.encode(encoding='UTF-8',errors='strict')
irc.connect ( ( network, port ) )
print (irc.recv ( 4096 ))
irc.send (USER)
irc.send (PASS)
irc.send (JOIN)
while True:
data = irc.recv ( 4096 )
if data.find ( PING ) != -1:
irc.send ( PONG + data.split() [ 1 ] + '\r\n' )
print (data)
This happens because irc.rizon.net is a geobalanced DNS record. It checks where your bot is coming from and then automatically assigns it a server to connect to.
Basically there is no such server as 'irc.rizon.net', if you always want the same one (you don't) then just specify one of the servers you have listed.