From 66203bf1383ef3df1ec38840e49799329c4d49e4 Mon Sep 17 00:00:00 2001 From: yvesf Date: Wed, 27 May 2009 20:40:05 +0000 Subject: cool git-svn-id: http://xapek.org/svn/common/omegle@1016 d0e8fea9-7529-0410-93fb-d39fd5b9c1dd --- omegle.py | 155 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ proxy.py | 41 +++++++++++++++++ test.py | 152 ------------------------------------------------------------ 3 files changed, 196 insertions(+), 152 deletions(-) create mode 100644 omegle.py create mode 100644 proxy.py delete mode 100644 test.py diff --git a/omegle.py b/omegle.py new file mode 100644 index 0000000..f2efe8a --- /dev/null +++ b/omegle.py @@ -0,0 +1,155 @@ +import sys +import httplib +import urllib +import time +import json +from threading import Thread, Lock + +class Timer(Thread): + def __init__(self,func,interval=0.50): + Thread.__init__(self) + self.func = func + self.interval = interval + + self.running = False + + def run(self): + self.running = True + while self.running: + self.func() + time.sleep(self.interval) + + def stop(self): + self.running = False + + +class OmegleChat(object): + headers = { + "Host" : "omegle.com", + "Content-type": "application/x-www-form-urlencoded; charset=utf-8", + "Accept": "application/json" + } + + def __init__(self,poll_interval=0.5): + self.conn = httplib.HTTPConnection('www.omegle.com') +# self.conn.set_debuglevel(1) + + self.conn_lock = Lock() + + self.timer = Timer(self.events, poll_interval) + + def start(self): + self.conn_lock.acquire() + self.conn.request("POST", "/start", {}, OmegleChat.headers) + self.conn_lock.release() + r=self.conn.getresponse() + body=r.read() + id=body.split("\"") + if id.__len__() == 3: + self.id = id[1] + print "Connected, id=%s" % self.id + else: + raise Exception("Bad response: %s" % body) + #Poll events + self.timer.start() + + def disconnect(self): + self.timer.stop() + + self.conn_lock.acquire() + self.conn.request("POST", + "/disconnect", + urllib.urlencode({'id' : self.id}), + OmegleChat.headers) + r = self.conn.getresponse() + self.conn_lock.release() + body = r.read() + if body != "win": + raise Exception("/disconnect; Bad response: %s" % body) + + def events(self): + conn = httplib.HTTPConnection('www.omegle.com') + conn.request("POST", + "/events", + urllib.urlencode({'id' : self.id}), + OmegleChat.headers) + r=conn.getresponse() + body=r.read() + self.dispatch_event(conn,body) + conn.close() + + def send(self,msg): + self.conn_lock.acquire() + self.conn.request("POST", + "/send", + urllib.urlencode({'id':self.id,'msg':msg}), + OmegleChat.headers) + r=self.conn.getresponse() + self.conn_lock.release() + body = r.read() + if body != "win": + raise Exception("/send; Bad response %s" % body) + + def typing(self): + self.conn_lock.acquire() + self.conn.request("POST", + "/typing", + urllib.urlencode({'id':self.id}), + OmegleChat.headers) + r=self.conn.getresponse() + self.conn_lock.release() + body = r.read() + if body != "win": + raise Exception("/typing; Bad response %s" % body) + + + def dispatch_event(self,conn,body): + try: + data_set=json.read(body) + if not data_set: + print "No Data received" + return + for data in data_set: + if data[0] == "typing": + self.on_typing() + elif data[0] == "gotMessage" and data.__len__() == 2: + self.on_message(data[1]) + elif data[0] == "connected": + self.on_connect() + elif data[0] == "strangerDisconnected": + self.timer.stop() + self.on_disconnect() + elif data[0] == "waiting": + pass #unhandled + else: + print "Unknown JSON Data: %s" % body + except json.ReadException: + print "Json ReadException. Body: %s" % body + + def on_message(self,message): + print "<<< %s" % message + def on_connect(self): + print "Connected" + def on_typing(self): + print "Stranger is typing" + def on_disconnect(self): + print "Stranger Disconnectet" + + +if __name__ == "__main__": + print "Lets chat. Type \"quit\" to disconnect" + chat = OmegleChat() + chat.start() + while 1==1: + try: + cmd=sys.stdin.readline().strip() + except KeyboardInterrupt: + chat.disconnect() + break + if cmd=="quit": + chat.disconnect() + break + else: + print ">>> %s" % cmd + chat.send(cmd) + diff --git a/proxy.py b/proxy.py new file mode 100644 index 0000000..5c37bb7 --- /dev/null +++ b/proxy.py @@ -0,0 +1,41 @@ +from omegle import OmegleChat +import sys + + +class OmegleProxyChat(OmegleChat): + def __init__(self,name): + OmegleChat.__init__(self) + self.name = name + + def set_partner(self,partner): + self.partner = partner + + def on_message(self,message): + print "%s: %s" %(self.name, message) + self.partner.send(message) + def on_connect(self): + print "%s Connected" % self.name + def on_typing(self): + print "%s is typing" % self.name + self.partner.typing() + def on_disconnect(self): + print "%s disconnect" % self.name + self.partner.disconnect() + +A=OmegleProxyChat("A") +B=OmegleProxyChat("B") + +A.set_partner(B) +B.set_partner(A) + +A.start() +B.start() + +print "press enter to abort" +sys.stdin.readline() + +A.disconnect() +B.disconnect() + + + diff --git a/test.py b/test.py deleted file mode 100644 index 950ba8d..0000000 --- a/test.py +++ /dev/null @@ -1,152 +0,0 @@ -import sys -import httplib -import urllib -import time -import json -from threading import Thread, Lock - -class Timer(Thread): - def __init__(self,func,interval=0.50): - Thread.__init__(self) - self.func = func - self.interval = interval - - self.running = False - - def run(self): - self.running = True - while self.running: - self.func() - time.sleep(self.interval) - - def stop(self): - self.running = False - - -class OmegleChat(object): - headers = { - "Host" : "omegle.com", - "Content-type": "application/x-www-form-urlencoded; charset=utf-8", - "Accept": "application/json" - } - - def __init__(self,poll_interval=0.5): - self.conn = httplib.HTTPConnection('www.omegle.com') -# self.conn.set_debuglevel(100) - - self.conn_lock = Lock() - - self.timer = Timer(self.events, poll_interval) - - def start(self): - self.conn_lock.acquire() - self.conn.request("POST", "/start", {}, OmegleChat.headers) - self.conn_lock.release() - r=self.conn.getresponse() - body=r.read() - id=body.split("\"") - if id.__len__() == 3: - self.id = id[1] - print "Connected, id=%s" % self.id - else: - raise Exception("Bad response: %s" % body) - #Poll events - self.timer.start() - - def disconnect(self): - self.timer.stop() - - self.conn_lock.acquire() - self.conn.request("POST", - "/disconnect", - urllib.urlencode({'id' : self.id}), - OmegleChat.headers) - r = self.conn.getresponse() - self.conn_lock.release() - body = r.read() - if body != "win": - raise Exception("/disconnect; Bad response: %s" % body) - - def events(self): - conn = httplib.HTTPConnection('www.omegle.com') - conn.request("POST", - "/events", - urllib.urlencode({'id' : self.id}), - OmegleChat.headers) - r=conn.getresponse() - body=r.read() - self.dispatch_event(conn,body) - conn.close() - - def send(self,msg): - self.conn_lock.acquire() - self.conn.request("POST", - "/send", - urllib.urlencode({'id':self.id,'msg':msg}), - OmegleChat.headers) - r=self.conn.getresponse() - self.conn_lock.release() - body = r.read() - if body != "win": - raise Exception("/send; Bad response %s" % body) - - def typing(self,msg): - self.conn_lock.acquire() - self.conn.request("POST", - "/typing", - urllib.urlencode({'id':self.id}), - OmegleChat.headers) - r=self.conn.getresponse() - self.conn_lock.release() - body = r.read() - if body != "win": - raise Exception("/typing; Bad response %s" % body) - - - def dispatch_event(self,conn,body): - try: - data_set=json.read(body) - for data in data_set: - if data[0] == "typing": - self.on_typing() - elif data[0] == "gotMessage" and data.__len__() == 2: - self.on_message(data[1]) - elif data[0] == "connected": - self.on_connect() - elif data[0] == "strangerDisconnected": - self.timer.stop() - self.on_disconnect() - elif data[0] == "waiting": - pass #unhandled - else: - print "Unknown JSON Data: %s" % body - except json.ReadException: - print "Json ReadException. Body: %s" % body - - def on_message(self,message): - print "<<< %s" % message - def on_connect(self): - print "Connected" - def on_typing(self): - print "Stranger is typing" - def on_disconnect(self): - print "Stranger Disconnectet" - - -if __name__ == "__main__": - print "Lets chat. Type \"quit\" to disconnect" - chat = OmegleChat() - chat.start() - while 1==1: - try: - cmd=sys.stdin.readline().strip() - except KeyboardInterrupt: - chat.disconnect() - break - if cmd=="quit": - chat.disconnect() - break - else: - print ">>> %s" % cmd - chat.send(cmd) - -- cgit v1.2.1