def setup_io_between_server_client(mysocket: socket) -> None: ''' This is to setup 'read' and 'write' ''' input_stream = mysocket.makefile('r') output_stream = mysocket.makefile('w') return input_stream, output_stream
def setup_game(s: socket) -> None: try: socket_in = s.makefile('r') socket_out = s.makefile('w') socket_out.write('I32CFSP_HELLO '+user_id+'\r\n') socket_out.flush() reply = socket_in.readline() socket_out.write('AI_GAME\r\n') socket_out.flush() reply = socket_in.readline() print(reply) except: print('Connection Failed. Closing the socket.') s.close()
def setup_game(s: socket) -> None: try: socket_in = s.makefile('r') socket_out = s.makefile('w') socket_out.write('I32CFSP_HELLO ' + user_id + '\r\n') socket_out.flush() reply = socket_in.readline() socket_out.write('AI_GAME\r\n') socket_out.flush() reply = socket_in.readline() print(reply) except: print('Connection Failed. Closing the socket.') s.close()
def _bio(sock: socket, bio): timestamp = '' bio_msg = send_bio_processor(token, bio, timestamp) send = sock.makefile('w') send.write(bio_msg + '\r\n') # bio send.flush() response(sock)
def join(sock: socket, username, password): global token join_msg = send_join_processor(username, password) send = sock.makefile('w') send.write(join_msg + '\r\n') # bio send.flush() token = response(sock).token
def _bio(sock: socket, bio, nprofile): timestamp = '' # encryption and send back my public key to the server bio = nprofile.encrypt_entry(bio, token) bio_msg = send_bio_processor(nprofile.public_key, bio, timestamp) send = sock.makefile('w') send.write(bio_msg + '\r\n') # bio send.flush() response(sock)
def response(sock: socket) -> DataTuple: """ Respond to the user about detailed message retrieved from the server response JSON Sample server JSON response: {"response": {"type": "ok", "message": "Welcome back, ffyuanda", "token": "551db8b5-7adb-4f9c-b610-a8aecd5793b0"}} {"response": {"type": "error", "message": "Post rejected: invalid timestamp"}} :param sock: current connecting socket :return: a DataTuple that contains the info from the server response JSON. """ recv = sock.makefile('r') resp = recv.readline() # resolve the conflict of two modules importing each other from ds_client import test_mode if test_mode: print(resp) # DataTuple conversion resp = extract_json(resp) if resp.type == 'ok': if resp.message.startswith('Welcome back'): msg = color_mod.color_code("Successfully logged in.\n", 'ok') print('{}' 'Server says: {}'.format(msg, resp.message)) elif resp.message.startswith('Welcome to'): msg = color_mod.color_code("Successfully registered in.\n", 'ok') print('{}' 'Server says: {}'.format(msg, resp.message)) elif 'Bio' in resp.message: msg = color_mod.color_code("Bio successfully updated.\n", 'ok') print('{}' 'Server says: {}'.format(msg, resp.message)) elif 'Post' in resp.message: msg = color_mod.color_code("Post successfully sent.\n", 'ok') print('{}' 'Server says: {}'.format(msg, resp.message)) elif resp.type == 'error': msg = color_mod.color_code("An error occurs.\n", 'error') print('{}' 'Error message: {}'.format(msg, resp.message)) return resp
def __init__(self, sock: socket, addr): """Constructor. Args: sock: The inverter socket, which is assumed to be connected. addr: The inverter network address (currently not used). """ self.sock = sock self.sock_file = sock.makefile('rwb') self.addr = addr # Inverters should respond in around 1.5 seconds, setting a timeout # above that value will ensure that the application won't hang too # long when the inverter doesn't send anything. self.sock.settimeout(30.0)
def init(sock: socket) -> SMPConnection: ''' The init method should be called for every program that uses the SMP Protocol. The calling program should first establish a connection with a socket object, then pass that open socket to init. init will then create file objects to handle input and output. ''' try: f_send = sock.makefile('w') f_recv = sock.makefile('r') except: raise SMPProtocolError("Invalid socket connection") return SMPConnection(socket=sock, send=f_send, recv=f_recv)
def post(sock: socket, message): send = sock.makefile('w') for single_post in message: entry = single_post.get_entry() if test_mode: print(entry) timestamp = single_post.get_time() post_msg = send_post_processor(token, entry, timestamp) send.write(post_msg + '\r\n') # bio send.flush() # the server's message reception time interval must be long enough # and I set 1 for convenience, or it will print an error cause I'm # sending message to it too frequently. time.sleep(1) response(sock)
def post(sock: socket, message, nprofile): send = sock.makefile('w') for single_post in message: entry = single_post.get_title() + '\n' + single_post.get_entry() # encryption entry = nprofile.encrypt_entry(entry, token) if test_mode: print(entry) timestamp = single_post.get_time() # and send back my public key to the server post_msg = send_post_processor(nprofile.public_key, entry, timestamp) send.write(post_msg + '\r\n') # bio send.flush() # the server's message reception time interval must be long enough # and I set 1 for convenience, or it will print an error cause I'm # sending message to it too frequently. response(sock) time.sleep(1)