Skip to content
Snippets Groups Projects
Commit 973a67d9 authored by Per Cederqvist's avatar Per Cederqvist
Browse files

Documentation added.

(TcpBase): New class.  This contains most of the old Tcp class,
	except for the socket creating and connecting code.
(Tcp): Renamed to TcpBase.
(TcpClient): New class.
(TcpClient.__init__): New method.
parent ab64246f
Branches
Tags
No related merge requests found
"""Connect to a TCP socket.
This module provides the TcpClient class. It inherits Expectable
and can be used to communicate with a TCP/IP server.
"""
import socket import socket
import pcl_expect import pcl_expect
__all__ = [ __all__ = [
"Tcp", "TcpBase",
"TcpClient",
] ]
class Tcp(pcl_expect.Expectable): class TcpBase(pcl_expect.Expectable):
"""Connect to a TCP port."""
def __init__(self, sockaddr):
"""Connect a TCP socket to the address given by sockaddr.
sockaddr is passed to the connect() method of a socket """Communicate with a remote TCP port."""
object. It should normally be a pair of a host name and
port number.
def __init__(self, sock):
"""Communicate with sock, which should be a connected socket object.
""" """
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock = sock
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.connect(sockaddr)
pcl_expect.Expectable.__init__(self, self.sock.fileno()) pcl_expect.Expectable.__init__(self, self.sock.fileno())
def send(self, s): def send(self, s):
"""Send a string to the remote TCP port."""
self.sock.send(s) self.sock.send(s)
def close(self): def close(self):
"""Close the session."""
pcl_expect.Expectable.close(self) pcl_expect.Expectable.close(self)
self.sock.close() self.sock.close()
class TcpClient(TcpBase):
"""Connect to a remote TCP port."""
def __init__(self, sockaddr):
"""Connect a TCP socket to the address given by sockaddr.
sockaddr is passed to the connect() method of a socket
object. It should normally be a pair of a host name and
port number.
"""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.connect(sockaddr)
TcpBase.__init__(self, sock)
# FIXME (bug 1180): It would be nice to provide a TcpServer class as
# well, but it wouldn't fit in the current Expectable framework.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment