Menu

question about socket timeout error

Help
xu long
2008-05-29
2013-04-25
  • xu long

    xu long - 2008-05-29

    hi!

    I just can't get the right error symbol that when a socket time out error raised,in below code I just follow the API document but it seems not work.

    my code is below:
    **************************************************
    from lightblue import *

    s = socket()
    s.bind(("",0))
    advertise("My OBEX Service",s,OBEX)
    s.settimeout(1)
    try:
        obex.recvfile(s,"test.txt")
    except socket.timeout:
        print ">>>time out,receive failed!"   
    s.close()
    **************************************************

    what said in the API document
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    settimeout(timeout)

        settimeout(timeout)
        
        Set a timeout on socket operations.  'timeout' can be a float,
        giving in seconds, or None.  Setting a timeout of None disables
        the timeout feature and is equivalent to setblocking(1).
        Setting a timeout of zero is the same as setblocking(0).   
       
        If a timeout is set, the connect, accept, send and receive operations will raise ((( socket.timeout))) if a timeout occurs.

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        Best Regards!

     
    • xu long

      xu long - 2008-05-29

      when I run the code in terminal,it give me message like this:

      Traceback (most recent call last):
        File "test.py", line 30, in <module>
          except socket.timeout:
      AttributeError: 'function' object has no attribute 'timeout'

      But I find  socket object have the attribute of timeout,in other's application I found the code "except socket.timeout",why it not working here?

       
    • xu long

      xu long - 2008-05-29

      The soft I have installed is bellow:
      openobex 1.3-3
      python-bluez 0.14-1
      python 2.5
      lightblue 0.3.2

      ubuntu 8.04

       
    • blam

      blam - 2008-06-03

      Hi,

      Sorry for the late reply.

      Try this instead:

      import lightblue
      import socket

      s = lightblue.socket()
      s.bind(("",0))
      lightblue.advertise("My OBEX Service",s, lightblue.OBEX)
      s.settimeout(1)
      try:
          lightblue.obex.recvfile(s,"test.txt")
      except socket.timeout:
          print ">>>time out,receive failed!"    
          s.close()

      The problem with the code you were trying was that it was trying to get the 'timeout' class from the 'socket' function in lightblue, instead of the 'socket' module in the standard library. This code fixes the problem by explicitly stating where 'timeout' comes from.

       
    • xu long

      xu long - 2008-06-10

      thank you Blam,I followed what you said and get the result below:

      Traceback (most recent call last):
        File "aa.py", line 11, in <module>
          lightblue.obex.recvfile(s,"test.txt") 
        File "/usr/lib/python2.5/site-packages/lightblue/_obex.py", line 368, in recvfile
          conn, addr = sock.accept()
        File "/usr/lib/python2.5/site-packages/lightblue/_lightblue.py", line 161, in accept
          raise _socket.error(str(e))
      socket.error: timed out

      Source Code is :
      #!/usr/bin/python
      #-*-coding:utf8-*-

      import lightblue
      import socket

      s = lightblue.socket() 

      s.bind(("",0)) 

      lightblue.advertise("My OBEX Service",s, lightblue.OBEX) 

      s.settimeout(1) 

      try: 

          lightblue.obex.recvfile(s,"test.txt") 

      except socket.timeout: 

          print ">>>time out,receive failed!" 

      s.close()

      it seems not work correctly,and I'm really new to Linux,please help me,thanks!

       
      • blam

        blam - 2008-06-14

        Darn, that is a bug in LightBlue.

        I'll fix it in the next release. In the meantime, you can get it working in your own version by fixing src/linux/_lightblue.py. Just change lines 160-163 from this:

                except _bluetooth.error, e:
                    raise _socket.error(str(e))
                except _bluetooth.timeout, te:
                    raise _socket.timeout(str(te))

        to this:

                except _bluetooth.timeout, te:
                    raise _socket.timeout(str(te))
                except _bluetooth.error, e:
                    raise _socket.error(str(e))

        Thanks for finding this bug.

         
    • xu long

      xu long - 2008-06-14

      Yeah,It works,thank you Blam!^_^

       

Log in to post a comment.