Exercise 09 code

exer09a/main_client.py

'''
Simple echo client.
'''

import logging
log = logging.getLogger(__name__)
import socket

host = "localhost" # where to connect
port = 12345
buf_size = 1024

def run():
  ## Setup client socket
  s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  s.connect((host,port))
  ## Communicate
  s.send("echo data")
  log.debug("Sending")
  data = s.recv(buf_size)
  if data:
    log.debug("Received: " + data)
  s.close()

if __name__ == '__main__':
  logging.basicConfig(level=logging.DEBUG)
  run()

exer09a/main_server.py

'''
Simple echo server.
'''

import logging
log = logging.getLogger(__name__)
import socket

host = ""
port = 12345
buf_size = 1024

def run():
  ## Create and bind server socket
  s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  s.bind((host,port))
  s.listen(5)
  ## Hang on infinite loop accepting connections and echoing data
  while True:
    log.debug("Waiting connection ..")
    client, addr = s.accept()
    log.debug("Connection from: " + str(addr))
    data = client.recv(buf_size)
    if data:
      client.send(data)
    client.close()

if __name__ == '__main__':
  logging.basicConfig(level=logging.DEBUG)
  run()

exer09b/main_client.py

'''
Simple echo client.
'''

import logging
log = logging.getLogger(__name__)
from tcp.interfaces import *
from tcp.tcp_usage import *
import select

class Client(TCP_ind):
  
  def __init__(self,tcp):
    self.tcp = tcp
    self.conn = None
    self._done = False
    tcp.set_ind(self)

  def run(self):
    self.conn = self.tcp.connect("localhost",12345)
    self.conn.set_ind(self)
    log.debug("Sending ..")
    self.conn.send("test echo data")
    while not self._done:
      select.select([],[],[],1.0)

  def recv(self,data):
    log.debug("Receiving: " + data)
    self.conn.close()
    self._done = True
  
def run():
  tcp = TCP()
  client = Client(tcp)
  tcp.start()
  client.run()
  tcp.shutdown()

if __name__ == '__main__':
  logging.basicConfig(level=logging.DEBUG)
  run()

exer09b/main_server.py

'''
Simple echo server.
'''

import logging
log = logging.getLogger(__name__)
from tcp.interfaces import *
from tcp.tcp_usage import *

class Service(TCP_conn_ind):

  def __init__(self,server,conn):
    self.server = server
    self.conn = conn
    self.conn.set_ind(self)
  
  def recv(self,data):
    self.conn.send(data)

  def close(self):
    log.debug("connection closed")
    self.conn.close()
    self.server.remove_service(self)

class Server(TCP_ind):
  
  def __init__(self,tcp):
    self.tcp = tcp
    tcp.set_ind(self)
    self.services = list()

  def new_connection(self,conn):
    log.debug("new connection")
    service = Service(self,conn)
    self.services.append(service)
  
  def remove_service(self,service):
    self.services.remove(service)
  
def run():
  tcp = TCP()
  tcp.be_server(12345)
  server = Server(tcp)
  tcp.start()

if __name__ == '__main__':
  logging.basicConfig(level=logging.DEBUG)
  run()
Last modified: 2013/07/01 14:42