[newbie] trying socket as a replacement for nc

Jean Dubois jeandubois314 at gmail.com
Mon Dec 16 04:40:08 EST 2013


Op donderdag 12 december 2013 22:23:22 UTC+1 schreef Dan Stromberg:
> On Thu, Dec 12, 2013 at 12:28 AM, Jean Dubois <jeandubois314 at gmail.com> wrote:
> > On Thursday, December 12, 2013 12:20:36 AM UTC+1, Dan Stromberg wrote:
> >> On Wed, Dec 11, 2013 at 3:08 PM, Jean Dubois <jeandu... at gmail.com> wrote:
> >>
> >> I have an ethernet-rs232 adapter which allows me to connect to a measurement instrument by means of netcat on a linux system.
> >>
> >>
> >> e.g. entering nc 10.128.59.63 7000
> >>
> >> allows me to enter e.g.
> >>
> >> *IDN?
> >>
> >> after which I get an identification string of the measurement instrument back.
> >>
> >> I thought I could accomplish the same using the python module "socket"
> >>
> >> and tried out the sample program below which doesn't work however:
> >>
> >>
> >>
> >> Sockets reserve the right to split one socket.send() into multiple socket.recv()'s on the other end of the communication, or to aggregate multiple socket.send()'s into a single socket.recv() - pretty much any way the relevant IP stacks and communications equipment feel like for the sake of performance or reliability.
> >>
> >>
> >> The confusing thing about this is, it won't be done on every transmission - in fact, it'll probably happen rather seldom unless you're on a heavy loaded network or have some MTU issues (see Path MTU Discovery, and bear in mind that paths can change during a TCP session).  But writing your code assuming it will never happen is a bad idea.
> >>
> >>
> >>
> >> For this reason, I wrote http://stromberg.dnsalias.org/~strombrg/bufsock.html , which abstracts away these complications, and actually makes things pretty simple.  There are examples on the web page.
> >>
> >>
> >>
> >> HTH
> >
> > Dear Dan,
> > Could you copy paste here the code for your function I have to add to my "program"?
> This is untested, but it should be something like the following:
> #!/usr/bin/env python
> """
> A simple echo client
> """
> import socket as socket_mod
> import bufsock as bufsock_mod
> host = '10.128.59.63'
> port = 7000
> size = 10
> socket = socket_mod.socket(socket.AF_INET, socket.SOCK_STREAM)
> socket.connect((host,port))
> bufsock = bufsock_mod.bufsock(socket)
> bufsock.send('*IDN?')
> data = bufsock.recv(size)
> bufsock.close()
> print 'Received:', data
> You might look over
> http://stackoverflow.com/questions/19918307/retrieve-file-information-located-on-a-different-application-server-using-python/19918706#19918706
> for a more complete example.
So this is what I did:
1. svn checkout http://stromberg.dnsalias.org/svn/bufsock/
2. cd ~/bufsock/trunk
3. I made this test-file "buftest.py" with the following contents:
#!/usr/bin/env python

"""
A simple echo client
"""
import socket as socket_mod
import bufsock as bufsock_mod
host = '10.128.59.63'
port = 7000
size = 10
socket = socket_mod.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.connect((host,port))
bufsock = bufsock_mod.bufsock(socket)
bufsock.send('*IDN?')
data = bufsock.recv(size)
bufsock.close()
print 'Received:', data 

4. chmod +x buftest.py
5. ./buftest.py
6. This results in the following error message:
Traceback (most recent call last):
  File "./buftest.py", line 11, in <module>
    socket = socket_mod.socket(socket.AF_INET, socket.SOCK_STREAM)
NameError: name 'socket' is not defined

Probably there is still something wrong, can anyone here help me further?

kind regards,
jean



More information about the Python-list mailing list