From b20f49dfea22835e03404c68bc78cc6344b40300 Mon Sep 17 00:00:00 2001 From: yvesf Date: Tue, 9 Jun 2009 18:30:34 +0000 Subject: blah git-svn-id: http://xapek.org/svn/common/omegle@1092 d0e8fea9-7529-0410-93fb-d39fd5b9c1dd --- chat.py | 16 +++++++++------- omegle.py | 27 ++++++++++++++++----------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/chat.py b/chat.py index be36aec..5a78346 100644 --- a/chat.py +++ b/chat.py @@ -78,8 +78,8 @@ class ConversationWindow(object): pass class Omegle(OmegleChat): - def __init__(self,chatwindow): - OmegleChat.__init__(self) + def __init__(self,chatwindow,host="www.omegle.com"): + OmegleChat.__init__(self,host=host) self.chatwindow = chatwindow def on_message(self,message): @@ -96,9 +96,10 @@ class Omegle(OmegleChat): self.chatwindow.add_message("Server send Wait") class Chat(ConversationWindow): - def __init__(self): + def __init__(self,host="www.omegle.com"): + print "Chat(host=%s)" % (host) ConversationWindow.__init__(self) - self.omegle = Omegle(self) + self.omegle = Omegle(self,host=host) def on_toggle_connect(self): if self.omegle.is_connected: @@ -121,9 +122,10 @@ class Chat(ConversationWindow): else: self.add_message("Not connected/confirmed") -import logging -logging.basicConfig(filename="chat.py.log",level=logging.DEBUG) -Chat().main() +if __name__ == "__main__": + import logging + logging.basicConfig(filename="chat.py.log",level=logging.DEBUG) + Chat().main() """ Unterstuetzt im moment noch nicht das senden des /typing diff --git a/omegle.py b/omegle.py index fd4caef..4f5e327 100644 --- a/omegle.py +++ b/omegle.py @@ -39,26 +39,31 @@ class Timer(Thread): class RESTClient(object): """Some routines used for calling the webservice""" headers = { - "Host" : "omegle.com", "Content-type": "application/x-www-form-urlencoded; charset=utf-8", "Accept": "application/json" } def __init__(self,host,http_debuglevel=0): + self.host=host self.conn_lock = Lock() self.conn = httplib.HTTPConnection(host) self.conn.set_debuglevel(http_debuglevel) def request(self,method,path,params): self.conn_lock.acquire() - self.conn.request(method,path,params,RESTClient.headers) - resp = self.conn.getresponse() - self.conn_lock.release() + try: + headers = RESTClient.headers.copy() + headers['Host'] = self.host + self.conn.request(method,path,params,headers) + resp = self.conn.getresponse() + finally: + self.conn_lock.release() return resp class OmegleChat(RESTClient): - def __init__(self,poll_interval=0.5,name=""): - RESTClient.__init__(self,'www.omegle.com') + def __init__(self,poll_interval=0.5,name="",host="omegle.com"): + self.name = name + RESTClient.__init__(self,host) self.timer = Timer(self._events, poll_interval) self.logger = logging.getLogger(__name__ + "." + self.__class__.__name__ + name) @@ -68,7 +73,7 @@ class OmegleChat(RESTClient): def start(self): """Starts a chat session. You have to wait until is_confirmed is set (or use on_connect)""" - self.logger.debug("/start") + self.logger.debug("/start host=%s" % self.host) if self.is_connected: self.logger.error("Already connected") raise Exception("Already connected") @@ -103,7 +108,7 @@ class OmegleChat(RESTClient): def _events(self): """does use its own "HTTPConnection" because its called async from a thread""" - conn = httplib.HTTPConnection('www.omegle.com') + conn = httplib.HTTPConnection(self.host) conn.request("POST", "/events", urllib.urlencode({'id' : self.id}), @@ -193,13 +198,13 @@ class OmegleChat(RESTClient): """To be overwritten""" self.logger.info("Server sent [\"waiting\"]") -def get_count(): +def get_count(host="omegle.com"): """Return the number of current online omegle users""" headers = { - "Host" : "omegle.com", + "Host" : host, "Content-type": "application/x-www-form-urlencoded; charset=utf-8", "Accept": "application/json"} - conn = httplib.HTTPConnection('www.omegle.com') + conn = httplib.HTTPConnection(host) conn.request("GET", "/count", urllib.urlencode({}), -- cgit v1.2.1