-
Notifications
You must be signed in to change notification settings - Fork 2
Client.py and error no 35 fix #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| import socket, sys, logging, urllib.parse, json | ||
|
|
||
| logging.basicConfig(filename='client_log.log', | ||
| format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', | ||
| level=logging.DEBUG) | ||
| logger = logging.getLogger(__name__) | ||
|
|
||
| class client: | ||
|
|
||
| DEFAULT_IP = 'localhost' | ||
| DEFAULT_PORT = 36577 | ||
|
|
||
| def __init__(self,ip=DEFAULT_IP,port=DEFAULT_PORT): | ||
| self.ip = ip | ||
| self.port = port | ||
| server_address = (ip, port) | ||
| logger.debug('Client instance created with IP %s port %s.' % server_address) | ||
|
|
||
| def __connect_socket(self): | ||
| # Create a TCP/IP socket | ||
| sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
| sock.settimeout(2) | ||
|
|
||
| # Connect the socket to the port where the server is listening | ||
| server_address = (self.ip, self.port) | ||
| logger.debug('connecting to %s port %s' % server_address) | ||
| sock.connect(server_address) | ||
|
|
||
| return sock | ||
|
|
||
| def __close_socket(self,sock): | ||
| logger.debug('closing socket') | ||
| sock.close() | ||
|
|
||
| def __recv(self,sock,delim='\n',recv_buffer=4096): | ||
| buffer = '' | ||
| while True: | ||
| data = urllib.parse.unquote_plus(sock.recv(recv_buffer).decode()) | ||
| assert data, 'Srver disconnected while receiving.' | ||
| buffer += data | ||
| if data[-1] == delim: | ||
| msg = json.loads(buffer[0:-len(delim)]) # Remove delims | ||
| if msg['error']: | ||
| raise Exception('Server Error: '+msg['response']+'\n'+msg['traceback']) | ||
| else: | ||
| return msg['response'] | ||
|
|
||
| def com(self,module,funcname,*args): | ||
| # Server always replies and always closes connection after msg | ||
| # assert funcname is a string, and cast varargin (cell array) | ||
| # to strings (use cellfun - operates on each entry of cell) | ||
|
|
||
| # last input is the keep_alive; for now, functionality not | ||
| # included | ||
| assert isinstance(module,str), 'module must be a string' | ||
| assert isinstance(funcname,str), 'funcname must be a string' | ||
|
|
||
| # Prepare both parts of message in case one errors | ||
| handshake = json.dumps({"name":module}) | ||
| message = json.dumps({"function":funcname, | ||
| "args":args, | ||
| "keep_alive":False}) | ||
|
|
||
| sock = self.__connect_socket() | ||
|
|
||
| resp = None | ||
|
|
||
| try: | ||
| # Send handshake | ||
| logger.debug('sending "%s"' % handshake) | ||
| sock.sendall((urllib.parse.quote_plus(handshake)+'\n').encode()) | ||
|
|
||
| # Look for the response and check if acknowledgement is received | ||
| resp = self.__recv(sock) | ||
| logger.debug('received "%s"' % resp.strip()) | ||
| assert resp == 'ack', 'Wasn\'t able to get an acknowledgement from the server' | ||
|
|
||
| # Send data | ||
| logger.debug('sending "%s"' % message) | ||
| sock.sendall((urllib.parse.quote_plus(message)+'\n').encode()) | ||
|
|
||
| # Look for the response | ||
| resp = self.__recv(sock) | ||
| logger.debug('received "%s"' % resp.strip()) | ||
|
|
||
| finally: | ||
| self.__close_socket(sock) | ||
| return resp | ||
|
|
||
| def help(self): | ||
| handshake = json.dumps({"name":"_help"}) | ||
|
|
||
| sock = self.__connect_socket() | ||
|
|
||
| resp = None | ||
|
|
||
| try: | ||
| # Send handshake | ||
| logger.debug('sending "%s"' % handshake) | ||
| sock.sendall((urllib.parse.quote_plus(handshake)+'\n').encode()) | ||
|
|
||
| # Look for the response | ||
| resp = self.__recv(sock) | ||
| logger.debug('received "%s"' % resp.strip()) | ||
|
|
||
| finally: | ||
| self.__close_socket(sock) | ||
| return resp | ||
|
|
||
| def ping(self): | ||
| handshake = json.dumps({"name":"_ping"}) | ||
|
|
||
| sock = self.__connect_socket() | ||
|
|
||
| resp = None | ||
|
|
||
| try: | ||
| # Send handshake | ||
| logger.debug('sending "%s"' % handshake) | ||
| sock.sendall((urllib.parse.quote_plus(handshake)+'\n').encode()) | ||
|
|
||
| # Look for the response | ||
| resp = self.__recv(sock) | ||
| logger.debug('received ["%s",%s]' % (resp[0], resp[1])) | ||
|
|
||
| finally: | ||
| self.__close_socket(sock) | ||
| return resp | ||
|
|
||
| def reload(self,module): | ||
| assert isinstance(module,str), 'module must be a string' | ||
|
|
||
| handshake = json.dumps({"name":"_reload_"+module}) | ||
|
|
||
| sock = self.__connect_socket() | ||
|
|
||
| resp = None | ||
|
|
||
| try: | ||
| # Send handshake | ||
| logger.debug('sending "%s"' % handshake) | ||
| sock.sendall((urllib.parse.quote_plus(handshake)+'\n').encode()) | ||
|
|
||
| # Look for the response | ||
| resp = self.__recv(sock) | ||
| logger.debug('received "%s"' % resp.strip()) | ||
|
|
||
| finally: | ||
| self.__close_socket(sock) | ||
| return resp | ||
|
|
||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be worth checking that the response is the expected response.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean checking if the response is "ack"?
I added a line "assert resp == 'ack', '%s does not exist' % module". Let me know if that works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep - except I would say a more general error; "Wan't able to get an acknowledgement from the server" (your error message makes an assumption that we don't need to make)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it! Just changed it.