home *** CD-ROM | disk | FTP | other *** search
- ##########################################################################################
- ## ///////////////////////////////////////////////////////////////////////////////////////
- ## //
- ## // socket.py
- ## //
- ## // Authors: Matthφas ┴sgeirsson
- ## // Created: August 2002
- ## // Project: EVE
- ## //
- ## // Description:
- ## //
- ## // A wrapper to give NetClient.RawSocket the same (for most part)
- ## // interface as the standard python socket class
- ## // to be implemented as needed
- ## //
- ## // Dependencies:
- ## //
- ## // PyAIO or NetClient
- ## //
- ## //
- ## // (c) CCP 2002
- ## //
- ## //
-
-
- import blue
-
- socketType = ""
-
- if boot.role == "client":
- Import("NetClient")
- socketType = "NetClient.AsyncSocket"
- else:
- Import("PyAIO")
- socketType = "PyAIO.Socket"
-
-
- # put in commonly used constants, just give them some values
- # since they will be ignored.
- AF_INET = 1
- SOCK_STREAM = 2
-
- import net
- import sys
-
- error = net.SocketClosed
-
- LINE_DELIMETER = "\r\n"
-
-
-
- #
- # Utility functions, implemented as needed
- #
- def getaddrinfo(host, port, something, stream):
- return [[AF_INET, SOCK_STREAM, 0, 0, (host,port)]]
-
- def getfqdn(name = None):
- if name != None:
- raise "not implemented"
- if boot.role == "client":
- return NetClient.DNS().GetLocalHostname()
- else:
- return NetUtil.Util().GetLocalHostname()
-
-
- def gethostbyname(hostname):
- if boot.role == "client":
- return NetClient.DNS().LookupHostname(hostname)
- else:
- return NetUtil.Util().LookupHostname(hostname)
-
- # FileWrap
- # A wrapper which implements a file like interface, returned by
- # the makefile method in socket
- class FileWrap:
- def __init__(self, s):
- self.socket = s
- self.buff = ""
-
- def readline(self):
- try:
- while self.buff.find(LINE_DELIMETER) == -1:
- self.buff = self.buff + self.socket.recv()
-
- i = self.buff.find(LINE_DELIMETER)
- result = self.buff[:i]
- self.buff = self.buff[i+2:]
- return result
- except:
- return self.buff
-
- def readlines(self, num):
- ##print "FileWrap.readlines"
- # just ignore num and read all lines
- result = []
- try:
- while 1:
- result.append(self.readline())
- except socket.error, e:
- pass
- return result
-
- def read(self, bytes = sys.maxint):
- # there is a reason for this implementation
- # if the default value of bytes is used data
- # will be read from the connection until the connection
- # is closed. This is the only way to read from a HTTP/1.0
- # connection with the standard libraries.
- # The code I removed broke existing code. What code I don't know.
- # If it was your code, just let me know and we might be able
- # to find a solution which does not break existing code
- try:
- while len(self.buff) < bytes:
- self.buff += self.socket.recv()
- except:
- ret = self.buff
- self.buff = ""
- return ret
-
- result = ""
- if len(self.buff) <= bytes:
- result = self.buff
- self.buff = ""
- else:
- result = self.buff[:bytes]
- self.buff = self.buff[bytes:]
- return result
-
-
- def close(self):
- self.socket.close()
- self.socket = None
-
- class socket:
- def __init__(self, family_type = 0, stream = 0, notused = 0):
- global socketType
- self.ep = blue.os.CreateInstance(socketType)
- self.closed = 0 # this variable is only used to prevent close from being called
- # twice since that crashes the client when using httplib
-
- def connect(self, (addr, port)):
- self.ep.Connect(addr,port)
-
-
- def getpeername(self):
- return self.clientHostname
-
-
- def send(self, buffer, flags = 0):
- self.ep.Write(buffer)
-
- def sendall(self, buffer, flags = 0):
- self.ep.Write(buffer)
-
- def recv(self, size = 0, flags = 0):
- return self.ep.Read()
-
- def shutdown(self, how):
- self.ep.Shutdown()
-
- def close(self):
- if self.closed:
- return
- try:
- self.ep.Close()
- except:
- pass
- self.closed = 1
-
- def makefile(self, mode, bufsize=0):
- return FileWrap(self)
-
- # unipmlemented methods... implement as needed
- # (or change code which uses it)
- def libd(self, addr):
- raise AttributeError("libd is not implemented in lib\socket")
-
- def accept(self):
- raise AttributeError("accept is not implemented in lib\socket")
-
- def getsockname(self):
- raise AttributeError("getsockname is not implemented in lib\socket")
-
- def fileno():
- raise AttributeError("fileno is not implemented in lib\socket")
-
- def setblocking(self, flag):
- raise AttributeError("setblocking is not implemented in lib\socket")
-
- def setsockopt(self, optname, value):
- raise AttributeError("setsockopt is not implemented in lib\socket")
-