Sockets

Rafael Mentz Aquino rafael at zumnet.com.br
Fri Sep 14 10:19:36 EDT 2001


Hi, there,

I'm trying to create a socket script to work as server,
using the example from the lib reference manual,
with a little change that puts the script in looping,
so I can make more than one requisition without stopping
and starting the script all the time.

When I execute the script, it works fine, listening in the
given port and answering the requisitions...

But If I try to put the script in background (in FreeBSD)
I got an error saying (address already in use). I guess
its because of the while 1 looping...

The script is below.

Thanks!!!

Rafael Mentz Aquino

     1 #!/usr/local/bin/python
      2
      3 import socket, sys, string
      4
      5 data = ""
      6
      7 def sock():
      8    data = ""
      9
     10    HOST='127.0.0.1'
     11    PORT=50007
     12    s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
     13    s.bind((HOST,PORT))
     14    s.listen(1)
     15    s.setblocking(1)
     16    conn, addr = s.accept()
     17    print 'Connected by', addr
     18    while 1:
     19       data = conn.recv(1024)
     20
     21       if data == "quit" : sys.exit()
     22
     23       if data == "server": .... etc etc
     ....
    ....
     100     if not data:
     101               conn.close()
     102                break
     103  while not data == "quit":             ## It's a redundancy, I
know.. ;-)
     104         socks()
     105  conn.close()








More information about the Python-list mailing list