Beispiel #1
0
 def ntransfercmd(self, cmd, rest=None):
     timeout = self._timeout
     size = None
     if self.passiveserver:
         host, port = self.makepasv()
         tp = socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM)[0]
         af, socktype, proto, canon, sa = tp
         if timeout is None:
             conn = socket.socket(af, socktype, proto)
         else:
             conn = socket.safety_socket(timeout,af,socktype,proto)
         conn.connect(sa)
         if rest is not None:
             self.sendcmd("REST %s" % rest)
         resp = self.sendcmd(cmd)
         if resp[0] != '1':
             raise error_reply, resp
     else:
         sock = self.makeport()
         if rest is not None:
             self.sendcmd("REST %s" % rest)
         resp = self.sendcmd(cmd)
         if resp[0] != '1':
             raise error_reply, resp
         conn, sockaddr = sock.accept()
     if resp[:3] == '150':
         # this is conditional in case we received a 125
         size = parse150(resp)
     return conn, size
Beispiel #2
0
 def connect(self, host='localhost', port=0, timeout=None):
     if timeout is None:
         timeout = self._timeout
     self._timeout = timeout
     if not port and (host.find(':') == host.rfind(':')):
         i = host.rfind(':')
         if i >= 0:
             host, port = host[:i], host[i + 1:]
             try:
                 port = int(port)
             except ValueError:
                 raise socket.error, "nonnumeric port"
     if not port: port = SMTP_PORT
     if self.debuglevel > 0: print 'connect:', (host, port)
     msg = "getaddrinfo returns an empty list"
     self.sock = None
     for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
         af, socktype, proto, canonname, sa = res
         try:
             if timeout is None:
                 self.sock = socket.socket(af, socktype, proto)
             else:
                 self.sock = socket.safety_socket(timeout, af, socktype,
                                                  proto)
             if self.debuglevel > 0: print 'connect:', (host, port)
             self.sock.connect(sa)
         except socket.error, msg:
             if self.debuglevel > 0: print 'connect fail:', (host, port)
             if self.sock:
                 self.sock.close()
             self.sock = None
             continue
         break
Beispiel #3
0
 def connect(self,host='localhost',port=0,timeout=None):
     if timeout is None:
         timeout = self._timeout
     self._timeout = timeout
     if not port and (host.find(':') == host.rfind(':')):
         i = host.rfind(':')
         if i >= 0:
             host, port = host[:i], host[i+1:]
             try: port = int(port)
             except ValueError:
                 raise socket.error, "nonnumeric port"
     if not port: port = SMTP_PORT
     if self.debuglevel > 0: print 'connect:', (host, port)
     msg = "getaddrinfo returns an empty list"
     self.sock = None
     for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
         af, socktype, proto, canonname, sa = res
         try:
             if timeout is None:
                 self.sock = socket.socket(af, socktype, proto)
             else:
                 self.sock = socket.safety_socket(timeout,af,
                                                  socktype,proto)
             if self.debuglevel > 0: print 'connect:', (host, port)
             self.sock.connect(sa)
         except socket.error, msg:
             if self.debuglevel > 0: print 'connect fail:', (host, port)
             if self.sock:
                 self.sock.close()
             self.sock = None
             continue
         break
Beispiel #4
0
 def connect(self, timeout=None):
     if timeout is None:
         timeout = self._timeout
     self._timeout = timeout
     msg = "getaddrinfo returns an empty list"
     for res in socket.getaddrinfo(self.host, self.port, 0,
                                   socket.SOCK_STREAM):
         af, socktype, proto, canonname, sa = res
         try:
             if self._timeout is None:
                 self.sock = socket.socket(af, socktype, proto)
             else:
                 self.sock = socket.safety_socket(self._timeout, af,
                                                  socktype, proto)
             if self.debuglevel > 0:
                 print "connect: (%s, %s)" % (self.host, self.port)
             self.sock.connect(sa)
         except socket.error, msg:
             if self.debuglevel > 0:
                 print 'connect fail:', (self.host, self.port)
             if self.sock:
                 self.sock.close()
             self.sock = None
             continue
         break
 def _get_ready_socket(self):
     sock = socket.safety_socket(self.timeout, socket.AF_INET,
                                 socket.SOCK_STREAM)
     self.msglog('Going to connect to server')
     sock.connect((self._server, self._port))
     http_sock = sock
     if self._secure:
         realsock = sock
         if hasattr(sock, '_sock'):
             realsock = sock._sock
         ssl = socket.ssl(realsock, properties.PRIVATE_KEY_FILE,
                          properties.CERTIFICATE_PEM_FILE)
         http_sock = FakeSocket(sock, ssl)
     return http_sock
Beispiel #6
0
 def connect(self, timeout=None):
     if timeout is None:
         timeout = self._timeout
     self._timeout = timeout
     if self._timeout is None:
         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     else:
         sock = socket.safety_socket(self._timeout, socket.AF_INET,
                                     socket.SOCK_STREAM)
     sock.connect((self.host, self.port))
     realsock = sock
     if hasattr(sock, '_sock'):
         realsock = sock._sock
     ssl = socket.ssl(realsock, self.key_file, self.cert_file)
     self.sock = FakeSocket(sock, ssl)
 def _get_ready_socket(self):
     sock = socket.safety_socket(self.timeout, socket.AF_INET, socket.SOCK_STREAM)
     self.msglog('Going to connect to server')
     sock.connect((self._server, self._port))
     http_sock = sock
     if self._secure:
         realsock = sock
         if hasattr(sock, '_sock'):
             realsock = sock._sock
         ssl = socket.ssl(
             realsock, 
             properties.PRIVATE_KEY_FILE, 
             properties.CERTIFICATE_PEM_FILE
         )
         http_sock = FakeSocket(sock, ssl)
     return http_sock
Beispiel #8
0
 def connect(self,timeout=None):
     if timeout is None:
         timeout = self._timeout
     self._timeout = timeout
     if self._timeout is None:
         sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
     else:
         sock = socket.safety_socket(self._timeout,
                                     socket.AF_INET,
                                     socket.SOCK_STREAM)
     sock.connect((self.host,self.port))
     realsock = sock
     if hasattr(sock,'_sock'):
         realsock = sock._sock
     ssl = socket.ssl(realsock,self.key_file,self.cert_file)
     self.sock = FakeSocket(sock,ssl)
Beispiel #9
0
 def makeport(self):
     timeout = self._timeout
     msg = "getaddrinfo returns an empty list"
     sock = None
     for res in socket.getaddrinfo(None, 0, self.af, 
                                   socket.SOCK_STREAM, 0, 
                                   socket.AI_PASSIVE):
         af, socktype, proto, canonname, sa = res
         try:
             if timeout is None:
                 sock = socket.socket(af, socktype, proto)
             else:
                 sock = socket.safety_socket(timeout,af,
                                             socktype,proto)
             sock.bind(sa)
         except socket.error, msg:
             if sock:
                 sock.close()
             sock = None
             continue
         break
Beispiel #10
0
 def connect(self,host='',port=0,timeout=None):
     if timeout is None:
         timeout = self._timeout
     self._timeout = timeout
     if host: self.host = host
     if port: self.port = port
     self.passiveserver = 0
     msg = "getaddrinfo returns an empty list"
     for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM):
         af, socktype, proto, canonname, sa = res
         try:
             if timeout is None:
                 self.sock = socket.socket(af, socktype, proto)
             else:
                 self.sock = socket.safety_socket(timeout,af,
                                                  socktype,proto)
             self.sock.connect(sa)
         except socket.error, msg:
             if self.sock:
                 self.sock.close()
             self.sock = None
             continue
         break
Beispiel #11
0
 def connect(self,timeout=None):
     if timeout is None:
         timeout = self._timeout
     self._timeout = timeout
     msg = "getaddrinfo returns an empty list"
     for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM):
         af, socktype, proto, canonname, sa = res
         try:
             if self._timeout is None:
                 self.sock = socket.socket(af, socktype, proto)
             else:
                 self.sock = socket.safety_socket(self._timeout, 
                                                  af, socktype, proto)
             if self.debuglevel > 0:
                 print "connect: (%s, %s)" % (self.host, self.port)
             self.sock.connect(sa)
         except socket.error, msg:
             if self.debuglevel > 0:
                 print 'connect fail:', (self.host, self.port)
             if self.sock:
                 self.sock.close()
             self.sock = None
             continue
         break
Beispiel #12
0
 def transport(self, datastream, target):
     targeturl = _TargetURL(target)
     contenttype = getattr(datastream, "mimetype", self.content_type)
     header = "POST %s HTTP/1.1\r\n" % targeturl.get_url()
     header += "Host: %s:%s\r\n" % (targeturl.get_server(), targeturl.get_port())
     header += "Content-Type: %s\r\n" % contenttype
     header += "Connection: close\r\n"
     if isinstance(datastream, str):
         header += "Content-Length: %s\r\n" % len(datastream)
         producer = SimpleProducer(datastream)
     elif self.chunked_data:
         producer = ChunkedProducer(StreamingProducer(datastream))
         header += "Transfer-Encoding: chunked\r\n"
     else:
         if not isinstance(datastream, StringIO.StringIO):
             self.msglog("Reading data for transport")
             buffer = StringIO.StringIO()
             read = datastream.read(1024)
             while read:
                 buffer.write(read)
                 read = datastream.read(1024)
         else:
             buffer = datastream
         buffer.seek(0)
         producer = StreamingProducer(buffer)
         header += "Content-Length: %s\r\n" % buffer.len
         self.msglog("Data read, going to transport %s bytes" % buffer.len)
     header += "\r\n"
     s = socket.safety_socket(self.timeout, socket.AF_INET, socket.SOCK_STREAM)
     self.msglog("Going to connect to server")
     s.connect((targeturl.get_server(), targeturl.get_port()))
     self.msglog("Going to write to server")
     data = header
     # Doing output polls to make sure that we do not hang
     while data:
         try:
             data = data[s.send(data) :]
         except ETimeout:
             s.close()
             tp = msglog.types.WARN
             msglog.log(
                 "broadway.mpx.service.data.http_post_transporter",
                 tp,
                 "HTTP Post transport failed.  Send Timed out.",
             )
             raise
         if not data:
             data = producer.more()
     self.msglog("Going to read from server")
     response = StringIO.StringIO()
     # Doing input polls to make sure we dont hang.
     loop = 1
     while loop:
         try:
             data = s.recv(1024)
         except ETimeout:
             s.close()
             tp = msglog.types.WARN
             msglog.log(
                 "broadway.mpx.service.data.http_post_transporter",
                 tp,
                 "HTTP Post transport failed.  Receive Timed out.",
             )
             raise
         loop = len(data)
         response.write(data)
     s.close()
     self.msglog("Done reading from server")
     response.seek(0)
     headers = string.split(response.read(), "\r\n\r\n")
     while headers:
         header = headers.pop(0)
         if string.split(header, "\r\n")[0].find(" 200 ") >= 0:
             break
     else:
         response.seek(0)
         raise TypeError('Error response from %s: "%s"' % (response.read(), targeturl.get_target()))
     return
Beispiel #13
0
 def transport(self, datastream, target):
     targeturl = _TargetURL(target)
     contenttype = getattr(datastream, 'mimetype', self.content_type)
     header = 'POST %s HTTP/1.1\r\n' % targeturl.get_url()
     header += 'Host: %s:%s\r\n' % (targeturl.get_server(),
                                    targeturl.get_port())
     header += 'Content-Type: %s\r\n' % contenttype
     header += 'Connection: close\r\n'
     if isinstance(datastream, str):
         header += 'Content-Length: %s\r\n' % len(datastream)
         producer = SimpleProducer(datastream)
     elif self.chunked_data:
         producer = ChunkedProducer(StreamingProducer(datastream))
         header += 'Transfer-Encoding: chunked\r\n'
     else:
         if not isinstance(datastream, StringIO.StringIO):
             self.msglog('Reading data for transport')
             buffer = StringIO.StringIO()
             read = datastream.read(1024)
             while read:
                 buffer.write(read)
                 read = datastream.read(1024)
         else:
             buffer = datastream
         buffer.seek(0)
         producer = StreamingProducer(buffer)
         header += 'Content-Length: %s\r\n' % buffer.len
         self.msglog('Data read, going to transport %s bytes' % buffer.len)
     header += '\r\n'
     s = socket.safety_socket(self.timeout, socket.AF_INET,
                              socket.SOCK_STREAM)
     self.msglog('Going to connect to server')
     s.connect((targeturl.get_server(), targeturl.get_port()))
     self.msglog('Going to write to server')
     data = header
     # Doing output polls to make sure that we do not hang
     while data:
         try:
             data = data[s.send(data):]
         except ETimeout:
             s.close()
             tp = msglog.types.WARN
             msglog.log('broadway.mpx.service.data.http_post_transporter',
                        tp, 'HTTP Post transport failed.  Send Timed out.')
             raise
         if not data:
             data = producer.more()
     self.msglog('Going to read from server')
     response = StringIO.StringIO()
     # Doing input polls to make sure we dont hang.
     loop = 1
     while loop:
         try:
             data = s.recv(1024)
         except ETimeout:
             s.close()
             tp = msglog.types.WARN
             msglog.log('broadway.mpx.service.data.http_post_transporter',
                        tp,
                        'HTTP Post transport failed.  Receive Timed out.')
             raise
         loop = len(data)
         response.write(data)
     s.close()
     self.msglog('Done reading from server')
     response.seek(0)
     headers = string.split(response.read(), '\r\n\r\n')
     while headers:
         header = headers.pop(0)
         if string.split(header, '\r\n')[0].find(' 200 ') >= 0:
             break
     else:
         response.seek(0)
         raise TypeError('Error response from %s: "%s"' %
                         (response.read(), targeturl.get_target()))
     return