Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: error in except

Reply
Thread Tools

Re: error in except

 
 
Albert Hopkins
Guest
Posts: n/a
 
      02-05-2013


On Mon, Feb 4, 2013, at 04:49 PM, Rodrick Brown wrote:
> For the life of me I cant figure out why this exception is being thrown.
> How could I use pdb to debug this?
>
> $ python udp_local2.py server
> File "udp_local2.py", line 36
> except:
> ^
> SyntaxError: invalid syntax
>
>
> #!/usr/bin/env python
>
> import random, socket, sys
> s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
>
> MAX = 65535
> PORT = 1060
>
> if 2 <= len(sys.argv) <= 3 and sys.argv[1] == 'server':
> interface = sys.argv[2] if len(sys.argv) > 2 else ''
> s.bind((interface, PORT))
> print 'Listening at', s.getsockname()
> while True:
> data, address = s.recvfrom(MAX)
> if random.randint(0, 1):
> print 'The client at', address, 'says:', repr(data)
> s.sendto('Your data was %d bytes' % len(data), address)
> else:
> print 'Pretending to drop packet from', address
>
> elif len(sys.argv) == 3 and sys.argv[1] == 'client':
> hostname = sys.argv[2]
> s.connect((hostname, PORT))
> print 'Client socket name is', s.getsockname()
> delay = 0.1
> while True:
> s.send('This is another message')
> print 'Waiting up to', delay, 'seconds for a reply'
> s.settimeout(delay)
> try:
> data = s.recv(MAX)
> except socket.timeout:
> delay *= 2
> if delay > 2.0:
> raise RuntimeError('I think the server is down')
> except:
> raise
> else:
> break
> print 'The server says', repr(data)
> else:
> print >> sys.stderr, 'usage: %d server [<interfae>]' %
> sys.argv[0]
> print >> sys.stderr, ' or: %d client <host>' % sys.argv[0]
> sys.exit(2)
> --
> http://mail.python.org/mailman/listinfo/python-list


Your "except" statement is on the same level as "if delay > 2.0"..
effectively:

if delay > 2.0
...
except:

which is not valid syntax. Excepts go with try: blocks, not if: blocks.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: What is the difference between 'except IOError as e:' and 'except MRAB Python 0 11-18-2009 02:52 AM
What is the difference between 'except IOError as e:' and 'except Peng Yu Python 1 11-18-2009 02:38 AM
try -> except -> else -> except? David House Python 2 07-06-2009 05:48 PM
who is simpler? try/except/else or try/except Fabio Z Tessitore Python 5 08-13-2007 12:52 AM
converting a nested try/except statement into try/except/else John Salerno Python 20 08-11-2006 02:48 PM



Advertisments