[newbie] trying socket as a replacement for nc

Jean-Michel Pichavant jeanmichel at sequans.com
Mon Dec 16 05:42:07 EST 2013


> Did you try
> 
> import telnetlib
> 
> ?
> 
> Note that in the code above I forgot the EOF, which is very much
> dependent of the equipment itself.
> 
> You may have to write
> t.write('*IDN?\n')
> or
> t.write('IDN?\n\r')
> 
> JM


Additionally, here's the code we're using for our signal generators, just to give you a broad idea:

	def getError(self):
		error = self._extractRsp(self._sendCmd('SYST:ERR?', 10))
		if '"No error"' in error:
			return None
		else:
			return error

	def _sendCmd(self, aCmd, timeout):
		self.send(str(aCmd) + self.SEND_LFCR)

		waitPattern = [self.PROMPT]
		try:			
			index, _, rsp= self._telnet.expect(waitPattern, timeout)
		except  EOFError:
			self._logger.error('Connection unexpectedly closed while sending/reading/ data.')
			raise MxgError('Connection unexpectedly closed while sending the command "%s"' % aCmd)

		if index == -1:
			raise MxgError('Timeout occurred while sendind the command "%s"' % aCmd)

		return rs


	def _extractRsp(self, rawRsp):
		# the returned string should be something like '\r\n<response>\r\n<prompt>'
		# or '\r\n<prompt>'
		# tries to extract the response only, removing the additional carriage returns and prompt
		rawRsp = rawRsp.replace(self.READ_LFCR+self.PROMPT, '')
		if rawRsp.startswith(self.READ_LFCR):
			rawRsp = rawRsp[2:]
		return rawRs


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.


More information about the Python-list mailing list