Network cable freeze when removed

This is the place for queries that don't fit in any of the other categories.

Network cable freeze when removed

Postby ymmij12 » Fri Jun 14, 2013 6:33 am

Hello!
Is it somebody who know what to do to not freeze the program when i remove the network cable? I think i have something with the: conn , addr = s.accept().
I want the program to run elif data =='error' part all the time the network cable is gone.

Code: Select all
#!/usr/bin/env python
 
try:
     import RPi.GPIO as GPIO
except Runtimeerror:
     print("error importing RPi.GPIO! Try superuser privileges")
 
import sys
import socket
import time
 
HOST = ''
PORT = 5455
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
 
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
 
GPIO.output(18, 0)
 
while True:
     conn , addr = s.accept()
     print 'Client Conected', addr
     GPIO.output(12, 0)
     data = conn.recv(1024)
     if data == 'ok':
         GPIO.output(18, 0)
         print 'ok'
     elif data == 'alarms':
         GPIO.output(18, 1)
         print 'alarms'
     elif data == 'error':
         print 'communication broken'
         GPIO.output(12, 1)
         time.sleep(5)
     data = 'error'
     s.close
Last edited by micseydel on Fri Jun 14, 2013 6:38 am, edited 1 time in total.
Reason: Added code tags.
ymmij12
 
Posts: 3
Joined: Fri Jun 14, 2013 6:18 am

Re: Network cable freeze when removed

Postby micseydel » Fri Jun 14, 2013 6:39 am

I'm not sure how to solve your problem, but
Code: Select all
s.close

Doesn't actually do anything. You're referring to method, but not calling it. Perhaps you mean
Code: Select all
s.close()
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1506
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Network cable freeze when removed

Postby ymmij12 » Fri Jun 14, 2013 6:45 am

Can you time out the script somehow?? So after 1-2 sec the script goes to the next part?

conn , addr = s.accept()
ymmij12
 
Posts: 3
Joined: Fri Jun 14, 2013 6:18 am

Re: Network cable freeze when removed

Postby micseydel » Fri Jun 14, 2013 8:22 am

Code: Select all
s.settimeout(seconds)

before whatever line hangs.

Added:
Steps I took to figure this out, since I know nothing about socket programming:

In an interactive session:
Code: Select all
>>> from socket import socket
>>> help(socket.accept)
Help on method accept in module socket:
accept(self) unbound socket._socketobject method
    accept() -> (socket object, address info)
   
    Wait for an incoming connection.  Return a new socket representing the
    connection, and the address of the client.  For IP sockets, the address
    info is a pair (hostaddr, port).


Nothing interesting there. How about...
Code: Select all
>>> help(socket)
Help on class _socketobject in module socket:

class _socketobject(__builtin__.object)
 |  socket([family[, type[, proto]]]) -> socket object
 | 
 |  Open a socket of the given type.  The family argument specifies the
 |  address family; it defaults to AF_INET.  The type argument specifies
 |  whether this is a stream (SOCK_STREAM, this is the default)
 |  or datagram (SOCK_DGRAM) socket.  The protocol argument defaults to 0,
 |  specifying the default protocol.  Keyword arguments are accepted.
 | 
 |  A socket object represents one endpoint of a network connection.
 | 
 |  Methods of socket objects (keyword arguments not allowed):
 | 
 |  accept() -- accept a connection, returning new socket and client address
 |  bind(addr) -- bind the socket to a local address
 |  close() -- close the socket
 |  connect(addr) -- connect the socket to a remote address
 |  connect_ex(addr) -- connect, return an error code instead of an exception
 |  dup() -- return a new socket object identical to the current one [*]
 |  fileno() -- return underlying file descriptor
 |  getpeername() -- return remote address [*]
 |  getsockname() -- return local address
 |  getsockopt(level, optname[, buflen]) -- get socket options
 |  gettimeout() -- return timeout or None
 |  listen(n) -- start listening for incoming connections
 |  makefile([mode, [bufsize]]) -- return a file object for the socket [*]
 |  recv(buflen[, flags]) -- receive data
 |  recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)
 |  recvfrom(buflen[, flags]) -- receive data and sender's address
 |  recvfrom_into(buffer[, nbytes, [, flags])
 |    -- receive data and sender's address (into a buffer)
 |  sendall(data[, flags]) -- send all data
 |  send(data[, flags]) -- send data, may not send all of it
 |  sendto(data[, flags], addr) -- send data to a given address
 |  setblocking(0 | 1) -- set or clear the blocking I/O flag
 |  setsockopt(level, optname, value) -- set socket options
 |  settimeout(None | float) -- set or clear the timeout
 |  shutdown(how) -- shut down traffic in one or both directions
 | 
 |   [*] not available on all platforms!
 | 
 |  Methods defined here:
 | 
 |  __init__(self, family=2, type=1, proto=0, _sock=None)
 | 
 |  accept(self)
 |      accept() -> (socket object, address info)
 |     
 |      Wait for an incoming connection.  Return a new socket representing the
 |      connection, and the address of the client.  For IP sockets, the address
 |      info is a pair (hostaddr, port).
 | 
 |  bind(...)
 |      bind(address)
 |     
 |      Bind the socket to a local address.  For IP sockets, the address is a
 |      pair (host, port); the host must refer to the local host. For raw packet
 |      sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])
 | 
 |  close(self, _closedsocket=<class 'socket._closedsocket'>, _delegate_methods=('recv', 'recvfrom', 'recv_into', 'recvfrom_into', 'send', 'sendto'), setattr=<built-in function setattr>)
 |      close()
 |     
 |      Close the socket.  It cannot be used after this call.
 | 
 |  connect(...)
 |      connect(address)
 |     
 |      Connect the socket to a remote address.  For IP sockets, the address
 |      is a pair (host, port).
 | 
 |  connect_ex(...)
 |      connect_ex(address) -> errno
 |     
 |      This is like connect(address), but returns an error code (the errno value)
 |      instead of raising an exception when an error occurs.
 | 
 |  dup(self)
 |      dup() -> socket object
 |     
 |      Return a new socket object connected to the same system resource.
 | 
 |  fileno(...)
 |      fileno() -> integer
 |     
 |      Return the integer file descriptor of the socket.
 | 
 |  getpeername(...)
 |      getpeername() -> address info
 |     
 |      Return the address of the remote endpoint.  For IP sockets, the address
 |      info is a pair (hostaddr, port).
 | 
 |  getsockname(...)
 |      getsockname() -> address info
 |     
 |      Return the address of the local endpoint.  For IP sockets, the address
 |      info is a pair (hostaddr, port).
 | 
 |  getsockopt(...)
 |      getsockopt(level, option[, buffersize]) -> value
 |     
 |      Get a socket option.  See the Unix manual for level and option.
 |      If a nonzero buffersize argument is given, the return value is a
 |      string of that length; otherwise it is an integer.
 | 
 |  gettimeout(...)
 |      gettimeout() -> timeout
 |     
 |      Returns the timeout in seconds (float) associated with socket
 |      operations. A timeout of None indicates that timeouts on socket
 |      operations are disabled.
 | 
 |  listen(...)
 |      listen(backlog)
 |     
 |      Enable a server to accept connections.  The backlog argument must be at
 |      least 0 (if it is lower, it is set to 0); it specifies the number of
 |      unaccepted connections that the system will allow before refusing new
 |      connections.
 | 
 |  makefile(self, mode='r', bufsize=-1)
 |      makefile([mode[, bufsize]]) -> file object
 |     
 |      Return a regular file object corresponding to the socket.  The mode
 |      and bufsize arguments are as for the built-in open() function.
 | 
 |  sendall(...)
 |      sendall(data[, flags])
 |     
 |      Send a data string to the socket.  For the optional flags
 |      argument, see the Unix manual.  This calls send() repeatedly
 |      until all data is sent.  If an error occurs, it's impossible
 |      to tell how much data has been sent.
 | 
 |  setblocking(...)
 |      setblocking(flag)
 |     
 |      Set the socket to blocking (flag is true) or non-blocking (false).
 |      setblocking(True) is equivalent to settimeout(None);
 |      setblocking(False) is equivalent to settimeout(0.0).
 | 
 |  setsockopt(...)
 |      setsockopt(level, option, value)
 |     
 |      Set a socket option.  See the Unix manual for level and option.
 |      The value argument can either be an integer or a string.
 | 
 |  settimeout(...)
 |      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).
 | 
 |  shutdown(...)
 |      shutdown(flag)
 |     
 |      Shut down the reading side of the socket (flag == SHUT_RD), the writing side
 |      of the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR).
 | 
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 | 
 |  __weakref__
 |      list of weak references to the object (if defined)
 | 
 |  family
 |      the socket family
 | 
 |  proto
 |      the socket protocol
 | 
 |  recv
 | 
 |  recv_into
 | 
 |  recvfrom
 | 
 |  recvfrom_into
 | 
 |  send
 | 
 |  sendto
 | 
 |  type
 |      the socket type

>>> help(socket.settimeout)
Help on method settimeout:

settimeout(...) unbound socket._socketobject method
    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).


I Ctrl+F'd for "timeout". The first two strings weren't interesting, but then I found this
Code: Select all
|  settimeout(None | float) -- set or clear the timeout

I wondered if it wanted seconds or milliseconds, so
Code: Select all
>>> help(socket.settimeout)
Help on method settimeout:

settimeout(...) unbound socket._socketobject method
    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).

And there you go.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1506
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Network cable freeze when removed

Postby ymmij12 » Fri Jun 14, 2013 9:41 am

SWEET!! Did work :D
ymmij12
 
Posts: 3
Joined: Fri Jun 14, 2013 6:18 am


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], snippsat and 5 guests