removed useless prints, __main__ and added

license
This commit is contained in:
Dimitur Kirov 2005-07-30 10:58:11 +00:00
parent 5dd434c249
commit 651acf5797
1 changed files with 26 additions and 23 deletions

View File

@ -1,3 +1,24 @@
## common/socks5.py
##
## Gajim Team:
## - Yann Le Boulanger <asterix@lagaule.org>
## - Vincent Hanquez <tab@snarc.org>
## - Nikos Kouremenos <nkour@jabber.org>
## - Dimitur Kirov <dkirov@gmail.com>
##
## Copyright (C) 2003-2005 Gajim Team
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation; version 2 only.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
import socket import socket
import select import select
import fcntl import fcntl
@ -5,6 +26,7 @@ import struct
import sha import sha
class SocksQueue: class SocksQueue:
''' qwueue for all file requests objects '''
def __init__(self): def __init__(self):
self.connected = 0 self.connected = 0
self.readers = {} self.readers = {}
@ -12,6 +34,7 @@ class SocksQueue:
self.idx = 0 self.idx = 0
def add_receiver(self, sock5_receiver): def add_receiver(self, sock5_receiver):
''' add new file request '''
self.readers[self.idx] = sock5_receiver self.readers[self.idx] = sock5_receiver
sock5_receiver.queue_idx = self.idx sock5_receiver.queue_idx = self.idx
sock5_receiver.queue = self sock5_receiver.queue = self
@ -33,10 +56,9 @@ class SocksQueue:
return None return None
def process(self, timeout=0): def process(self, timeout=0):
print 'PROCESS' ''' process all file requests '''
for idx in self.readers.keys(): for idx in self.readers.keys():
receiver = self.readers[idx] receiver = self.readers[idx]
print 'PROCESS receiver ', receiver.connected, receiver.receiving
if receiver.connected: if receiver.connected:
if not receiver.receiving and receiver.pending_data(): if not receiver.receiving and receiver.pending_data():
receiver.get_file_contents(timeout) receiver.get_file_contents(timeout)
@ -44,7 +66,6 @@ class SocksQueue:
self.remove_receiver(idx) self.remove_receiver(idx)
def remove_receiver(self, idx): def remove_receiver(self, idx):
print 'REMOVE', idx
if idx != -1: if idx != -1:
if self.readers.has_key(idx): if self.readers.has_key(idx):
del(self.readers[idx]) del(self.readers[idx])
@ -63,7 +84,6 @@ class Socks5:
def connect(self): def connect(self):
self._sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM) self._sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._sock.connect((self.host, self.port)) self._sock.connect((self.host, self.port))
print 'self.host, self.port', self.host, self.port
self._sock.setblocking(True) self._sock.setblocking(True)
self._send=self._sock.sendall self._send=self._sock.sendall
self._recv=self._sock.recv self._recv=self._sock.recv
@ -102,21 +122,16 @@ class Socks5:
def disconnect(self): def disconnect(self):
""" Closes the socket. """ """ Closes the socket. """
#~ try:
print 'disconnect'
fcntl.fcntl(self._sock, fcntl.F_SETFL, 0); fcntl.fcntl(self._sock, fcntl.F_SETFL, 0);
self._sock.close() self._sock.close()
#~ except:
#~ pass
self.connected = False self.connected = False
def pending_data(self,timeout=0): def pending_data(self,timeout=0):
""" Returns true if there is a data ready to be read. """ """ Returns true if there is a data ready to be read. """
if self._sock is None: if self._sock is None:
return False return False
print 'self._sock:', self._sock, timeout, self._sock
try: try:
return select.select([self._sock],[],[],0)[0] return select.select([self._sock],[],[],timeout)[0]
except: except:
return False return False
@ -134,8 +149,6 @@ class Socks5:
if address_type == 0x03: if address_type == 0x03:
addrlen = ord(buff[4]) addrlen = ord(buff[4])
address, port= struct.unpack('!%dsH' % addrlen, buff[5:]) address, port= struct.unpack('!%dsH' % addrlen, buff[5:])
else:
print 'ADRRTYPE:', address_type
return (version, command, rsvd, address_type, addrlen, address, port) return (version, command, rsvd, address_type, addrlen, address, port)
@ -181,7 +194,6 @@ class Socks5Receiver(Socks5):
return return
else: else:
self.receiving = True self.receiving = True
print 'get_file_contents', self.file_props
if self.file_props is None or \ if self.file_props is None or \
self.file_props.has_key('file-name') is False: self.file_props.has_key('file-name') is False:
return return
@ -219,13 +231,4 @@ class Socks5Receiver(Socks5):
self.queue.remove_receiver(self.queue_idx) self.queue.remove_receiver(self.queue_idx)
if __name__ == '__main__': # TODO REMOVE above lines when ready
sock5 = Socks5(host = '127.0.0.1', port = 8010,
initiator = 'fgh@jabber.homeunix.net/Psi',
target = 'emi@jabber.homeunix.net/Gajim',
sid = 's5b_962e391e895248e3')
sock5.connect()
#~ res=sock5.receive()
#~ print 'res', res
#~ sock5.mainloop()