Socket Blocking problem

sree skodela at btinternet.com
Fri Jan 3 05:04:28 EST 2003


Hi guys

I am writing a small socket program in python. Here there are two progs. One
sits on system A and accepts connections, does something with the data that
came in. Othe sits on system B and sends commands to system A. Problem is I
want to be able to read if system A is busy or free from system B. Hence I
tried setblocking(0) but then it fails to start with error as below on
system A.

     Traceback (most recent call last):
     File "periph.py", line 39, in ?
     (con,addr)=s.accept()
     socket.error: (11, 'Resource temporarily unavailable')

Then I tried with no setblocking in system A and setblocking(0) on System B.
It connects but fails on command as below.

    master.py desk 2670
   ['master.py', 'desk', '2670']
   READ HELO
   Traceback (most recent call last):
   File "master.py", line 17, in ?
   s=conn()
   File "master.py", line 12, in conn
   s.connect((sys.argv[1],port))
   socket.error: (115, 'Operation now in progress')

If I don't use setblocking it works fine except when I want to send 'READ
HELO' to check if the system is available it waits until system is free.

What I want is to try 'READ HELO' and decide if system is free. If not do
something else.

code for System A is here :
usage : python system-a.py 2675 
#########################################################################
from socket import *
import os,string,sys
global res,status
res=0
status=0

port=int(sys.argv[1])
s=socket(AF_INET,SOCK_STREAM)
#s.setblocking(0)
s.bind((gethostname(),port))
s.listen(5)

def pex(cmd):
    global status
    f=os.system(cmd)
    if f==0:
        return "SUCC"
    else:
        return "FAIL"
    
def process(con,cmd):
    global res,status
    if cmd=="QUIT":
        con.close()
        raise SystemExit
    elif cmd=="HELO":
        con.send("HELO")
        return
    elif string.find(cmd,"EXEC")>=0:
        cmd=cmd[string.index(cmd,'EXEC')+len('EXEC')+1:]
        status='BUSY'
        res=pex(cmd)
        status='FREE'
        return
    elif cmd=="STAT":
        con.send(res+'-'+status)
        return
while 1:
    (con,addr)=s.accept()
    cmd=con.recv(1024)
    process(con,cmd)
    
###########################################################

code for system B is here
usage: python system-b.py 192.168.122.2 2675
-----------------------------------------------------------
from socket import *
import os,sys,string

global s
s=0
print sys.argv
port=int(sys.argv[2])

def conn():
    s=socket(AF_INET,SOCK_STREAM)
    s.setblocking(0)
    s.connect((sys.argv[1],port))
    return s

while 1:
    cmd=string.strip(raw_input())
    s=conn()
    if string.find(cmd,"EXEC")>=0:
        s.send(cmd)
    elif string.find(cmd,"READ")>=0:
        s.send(cmd[string.index(cmd,'READ')+len('READ')+1:])
        print s.recv(1024)
    else:
        s.send(cmd)
    s.close()
-----------------------------------------------------------

Any help would be greatly appreciated.

Cheerio
sreekant




More information about the Python-list mailing list