import sys import socket from struct import * class mysocket: '''demonstration class only - coded for clarity, not efficiency ''' def __init__(self, sock=None): if sock is None: self.sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM) else: self.sock = sock def connect(self, host, port): self.sock.connect((host, port)) def mysend(self, msg): totalsent = 0 while totalsent < len(msg): sent = self.sock.send(msg[totalsent:]) if sent == 0: raise RuntimeError("socket connection broken") totalsent = totalsent + sent def myreceive(self, maxlen): chunks = [] bytes_recd = 0 while bytes_recd < maxlen: chunk = self.sock.recv(min(maxlen - bytes_recd, 2048)) bytes_recd = bytes_recd + len(chunk) if chunk == '': print ("Number of chunks received so far:", len(chunks), bytes_recd) break #raise RuntimeError("socket connection broken") chunks.append(chunk) return ''.join(chunks) def serve(self, port): self.sock.bind(('', port)) self.sock.listen(2) conn, addr = self.sock.accept() print (conn, addr) self.conn = conn if __name__ == "__main__": if (sys.argv[1] == 't'): s = mysocket() if (len(sys.argv) == 3): s.connect(sys.argv[2], 5002) else: s.connect("localhost", 5002) s.conn = s.sock msg = s.myreceive(2*1024*1024) text_file = open("trace.log", "wb") text_file.write(msg) text_file.close() s.sock.shutdown(0) s.sock.close() elif (sys.argv[1] == 'i'): with open(sys.argv[2], "rb") as f: bytes = f.read(200000) # max 200K if bytes != "": s = mysocket() s.connect(sys.argv[3], 64) s.mysend(pack(" 3: s.mysend(pack("