Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #76852 > unrolled thread

socket issue with recv()

Started byArthur Clarck <aclarck5@gmail.com>
First post2014-08-23 01:23 -0700
Last post2014-08-24 07:59 +0200
Articles 4 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  socket issue with recv() Arthur Clarck <aclarck5@gmail.com> - 2014-08-23 01:23 -0700
    Re: socket issue with recv() Arthur Clarck <aclarck5@gmail.com> - 2014-08-23 01:39 -0700
    Re: socket issue with recv() Marko Rauhamaa <marko@pacujo.net> - 2014-08-23 11:46 +0300
    Re: socket issue with recv() dieter <dieter@handshake.de> - 2014-08-24 07:59 +0200

#76852 — socket issue with recv()

FromArthur Clarck <aclarck5@gmail.com>
Date2014-08-23 01:23 -0700
Subjectsocket issue with recv()
Message-ID<64392c10-28c5-4eee-93d2-7dd4edba56fe@googlegroups.com>
Hello,

I am starting socket scripting with python.
I do understand from the doc that a socket is bmocking by default.
I wrote 2 basics script to verify this behaviour.

my "tcp_server.py":


import socket, sys
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

HOST = '192.168.0.103'
PORT = 1060

s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
while True:
   s.listen(1)
   print 'Listening at', s.getsockname()
   sc, sockname = s.accept()
   print 'We have accepted a connection from', sockname
   print 'Socket connects', sc.getsockname(), 'and', sc.getpeername()
   while True:
      data = sc.recv(1024)
      if data:
         print 'Got from client:', repr(data)
         data = ''
      else:
         #raise EOFError('socket closed')
         print 'socket closed by remote client'
         sc.close()
         sys.exit()


and my "tcp_client.py"

import socket, sys, time
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

HOST = '192.168.0.103'
PORT = 1060

s.connect((HOST, PORT))
print 'Client has been assigned socket name', s.getsockname()
while True:
   data = raw_input('cmd:')
   if (data == '\x05'):
      print 'closing socket and exit'
      s.close()
      sys.exit()
   s.sendall(data)

When both are running everything what I type in the client is received by the server.
When nothing is sent from the client the server is waiting (socket is blocking)
for next datas.
If I close the client with Ctrl-E (or a kill) the socket on the server is getting a null value and the server exit.
So everything is fine. And everything is clear in my mind.

The problem I have now is the following.
I have a script to connect to some telecom service.
The script is forking (parent/child)
The child is only reading what comes from the remote server.
Here the problematic code:

total = ''
while True:
   data = s.recv(1024)
   total += data
   if (data == ''):
      print 'remote site is closed'
      s.close()
      sys.exit()

What is happening is that I got some datas from the remote site,
Something like 'Contacting BH: ...'
But directly followed by 'remote site is closed.
And if I remove the 2 lines (s.close;sys.exit) my script is looping
So it means data=='' 
This makes no sense at all.
Why is the socket not waiting for the next datas flow ??
Why is the socket not blocking ?


With Perl I just have to write:

  while (sysread($socket,$data,1024)){
     syswrite(STDOUT,$data)
  }

And it works perfectly.

Any hint about what I missed in Python ?

kr,
Arthur.

[toc] | [next] | [standalone]


#76853

FromArthur Clarck <aclarck5@gmail.com>
Date2014-08-23 01:39 -0700
Message-ID<657dd455-1c37-44ca-8909-19ee53ec462e@googlegroups.com>
In reply to#76852
I forgot to mention that with this code:

total = '' 
while True: 
   data = s.recv(1024) 
   total += data 
   if (total != ''): 
      print total 
      total = ''
 
Everything is fine.
I am still looping but I get the complete data flow sent by the remote server.
The data flow is not corrupted with extra characters.
But I do not detect the closing of the socket on the remote server.
Why is recv not blocking !!!


Le samedi 23 août 2014 10:23:14 UTC+2, Arthur Clarck a écrit :
> Hello,
> 
> 
> 
> I am starting socket scripting with python.
> 
> I do understand from the doc that a socket is bmocking by default.
> 
> I wrote 2 basics script to verify this behaviour.
> 
> 
> 
> my "tcp_server.py":
> 
> 
> 
> 
> 
> import socket, sys
> 
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> 
> 
> 
> HOST = '192.168.0.103'
> 
> PORT = 1060
> 
> 
> 
> s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
> 
> s.bind((HOST, PORT))
> 
> while True:
> 
>    s.listen(1)
> 
>    print 'Listening at', s.getsockname()
> 
>    sc, sockname = s.accept()
> 
>    print 'We have accepted a connection from', sockname
> 
>    print 'Socket connects', sc.getsockname(), 'and', sc.getpeername()
> 
>    while True:
> 
>       data = sc.recv(1024)
> 
>       if data:
> 
>          print 'Got from client:', repr(data)
> 
>          data = ''
> 
>       else:
> 
>          #raise EOFError('socket closed')
> 
>          print 'socket closed by remote client'
> 
>          sc.close()
> 
>          sys.exit()
> 
> 
> 
> 
> 
> and my "tcp_client.py"
> 
> 
> 
> import socket, sys, time
> 
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> 
> 
> 
> HOST = '192.168.0.103'
> 
> PORT = 1060
> 
> 
> 
> s.connect((HOST, PORT))
> 
> print 'Client has been assigned socket name', s.getsockname()
> 
> while True:
> 
>    data = raw_input('cmd:')
> 
>    if (data == '\x05'):
> 
>       print 'closing socket and exit'
> 
>       s.close()
> 
>       sys.exit()
> 
>    s.sendall(data)
> 
> 
> 
> When both are running everything what I type in the client is received by the server.
> 
> When nothing is sent from the client the server is waiting (socket is blocking)
> 
> for next datas.
> 
> If I close the client with Ctrl-E (or a kill) the socket on the server is getting a null value and the server exit.
> 
> So everything is fine. And everything is clear in my mind.
> 
> 
> 
> The problem I have now is the following.
> 
> I have a script to connect to some telecom service.
> 
> The script is forking (parent/child)
> 
> The child is only reading what comes from the remote server.
> 
> Here the problematic code:
> 
> 
> 
> total = ''
> 
> while True:
> 
>    data = s.recv(1024)
> 
>    total += data
> 
>    if (data == ''):
> 
>       print 'remote site is closed'
> 
>       s.close()
> 
>       sys.exit()
> 
> 
> 
> What is happening is that I got some datas from the remote site,
> 
> Something like 'Contacting BH: ...'
> 
> But directly followed by 'remote site is closed.
> 
> And if I remove the 2 lines (s.close;sys.exit) my script is looping
> 
> So it means data=='' 
> 
> This makes no sense at all.
> 
> Why is the socket not waiting for the next datas flow ??
> 
> Why is the socket not blocking ?
> 
> 
> 
> 
> 
> With Perl I just have to write:
> 
> 
> 
>   while (sysread($socket,$data,1024)){
> 
>      syswrite(STDOUT,$data)
> 
>   }
> 
> 
> 
> And it works perfectly.
> 
> 
> 
> Any hint about what I missed in Python ?
> 
> 
> 
> kr,
> 
> Arthur.

[toc] | [prev] | [next] | [standalone]


#76854

FromMarko Rauhamaa <marko@pacujo.net>
Date2014-08-23 11:46 +0300
Message-ID<87oavb8ufc.fsf@elektro.pacujo.net>
In reply to#76852
Arthur Clarck <aclarck5@gmail.com>:

> What is happening is that I got some datas from the remote site,
> Something like 'Contacting BH: ...'
> But directly followed by 'remote site is closed.

This works:

========================================================================
#!/usr/bin/env python3

import sys, socket, os

def main():
    s = socket.socket()
    s.connect(("mail.python.org", 25))
    if os.fork() > 0:
        s.close()
        os.wait()
        os._exit(0)
    while True:
        data = s.recv(50)
        sys.stderr.write("{}\n".format(repr(data)))
        if not data:
            break

if __name__ == "__main__":
    main()
========================================================================


Marko

[toc] | [prev] | [next] | [standalone]


#76923

Fromdieter <dieter@handshake.de>
Date2014-08-24 07:59 +0200
Message-ID<mailman.13372.1408859988.18130.python-list@python.org>
In reply to#76852
Arthur Clarck <aclarck5@gmail.com> writes:
> ...
> The problem I have now is the following.
> I have a script to connect to some telecom service.
> The script is forking (parent/child)
> The child is only reading what comes from the remote server.
> Here the problematic code:
>
> total = ''
> while True:
>    data = s.recv(1024)
>    total += data
>    if (data == ''):
>       print 'remote site is closed'
>       s.close()
>       sys.exit()
>
> What is happening is that I got some datas from the remote site,
> Something like 'Contacting BH: ...'
> But directly followed by 'remote site is closed.

Some services (such as older HTTP 1.0 services) are designed
for "one command per connection" mode. This means, they answer
a signle command and then close the connection.

>From what you describe, your service might belong to this class.
In this case, you would need to open a new connection whenever
you detect that the old connection was closed.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web