Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'exception': 0.03; 'syntax': 0.03; 'elif': 0.04; 'debug': 0.05; 'socket': 0.05; 'sys': 0.05; 'except:': 0.07; 'try:': 0.07; 'python': 0.09; '"if': 0.09; '0.1': 0.09; 'hostname': 0.09; 'instance.': 0.09; 'instances.': 0.09; 'port))': 0.09; 'sys.argv[0]': 0.09; 'sys.exit(2)': 0.09; 'cc:addr:python-list': 0.10; 'subject:error': 0.11; '>>': 0.16; '1):': 0.16; '2.0:': 0.16; 'delay,': 0.16; 'pdb': 0.16; 'syntax.': 0.16; 'syntaxerror:': 0.16; 'true:': 0.16; 'mon,': 0.16; 'wrote:': 0.17; 'feb': 0.19; 'followed': 0.20; 'import': 0.21; 'exceptions': 0.22; 'cc:2**0': 0.23; '>': 0.23; 'statement': 0.23; 'raise': 0.24; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'skip:[ 10': 0.26; 'am,': 0.27; 'skip:# 10': 0.27; 'message- id:@mail.gmail.com': 0.27; 'interface': 0.27; 'this?': 0.28; 'address)': 0.29; 'socket,': 0.29; 'url:mailman': 0.29; 'skip:& 10': 0.29; 'class': 0.29; 'classes': 0.30; 'figure': 0.30; 'url:python': 0.32; 'file': 0.32; 'could': 0.32; 'print': 0.32; 'url:listinfo': 0.32; 'another': 0.33; 'skip:& 20': 0.33; 'received:google.com': 0.34; 'server': 0.35; '2.0': 0.35; 'data,': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'except': 0.36; 'url:org': 0.36; 'client': 0.36; 'level': 0.37; 'being': 0.37; 'why': 0.37; 'drop': 0.37; 'received:209': 0.37; 'received:209.85.216': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'skip:" 10': 0.40; 'url:mail': 0.40; 'think': 0.40; 'your': 0.60; 'address': 0.60; 'skip:u 10': 0.60; 'more': 0.63; 'brown': 0.65; 'life': 0.66; 'address,': 0.79; '1060': 0.84; '2013': 0.84; 'effectively:': 0.84; 'random,': 0.84; 'thing,': 0.84; 'albert': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=jCJoTORiIsW7/o+GpHS+RcekqCxVSeEpEf7ktMjNCDM=; b=a5FrvEHDmhA5fvvtAmQO9dwsZJEKBcMCgEYkXMRZb+BER6XLXpK35M3grccQDGE0FR eZnyCrutf1pbrRBGlJdmz5RYdcedz+EB/qtXPRIvpC8t1udy0ldmQrtgCs/2MybeR6S4 biKXDjHFPvMefhA93cVBL3afi2aXUvUjoJGk/rPshiq86AQSOwPzQheBmL0WATi9X3DR 6kTJnLjvDNJoQfvXWVJjM7Q5bQKsegoEfygoEEQSc8DRN9XTGtU+Mp/lH05R8I5DfJW0 uSC+skFIai5G1iMqhDF4yNuSo+GGFakWPFUcinZnAknLQP4V3lEY1Kdt4R/q5keiqbUH ysSA== MIME-Version: 1.0 X-Received: by 10.224.193.202 with SMTP id dv10mr1094987qab.77.1360047109113; Mon, 04 Feb 2013 22:51:49 -0800 (PST) In-Reply-To: <1360025881.32270.140661186861365.09CB02A5@webmail.messagingengine.com> References: <1360025881.32270.140661186861365.09CB02A5@webmail.messagingengine.com> Date: Tue, 5 Feb 2013 12:21:49 +0530 Subject: Re: error in except From: Laxmikant Chitare To: Albert Hopkins Content-Type: multipart/alternative; boundary=20cf300fb4b3d1088004d4f4a420 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 186 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360050582 news.xs4all.nl 6884 [2001:888:2000:d::a6]:47187 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38171 --20cf300fb4b3d1088004d4f4a420 Content-Type: text/plain; charset=ISO-8859-1 One more thing, apart from what Albert mentioned. Exceptions must be classes or instances. In effect you cannot just do 'raise'. 'raise' statement must be followed by a class or an instance. On Tue, Feb 5, 2013 at 6:28 AM, Albert Hopkins wrote: > > > 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 []' % > > sys.argv[0] > > print >> sys.stderr, ' or: %d client ' % 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. > -- > http://mail.python.org/mailman/listinfo/python-list > --20cf300fb4b3d1088004d4f4a420 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
One more thing, apart from what Albert mentioned.
Exceptions must be classes or instances. In effect you cannot just d= o 'raise'. 'raise' statement must be followed by a class or= an instance.
=A0


On Tue, Feb 5, 2013 at 6:28 AM, Albert Hopkins <= ;marduk@letterb= oxes.org> wrote:


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 throw= n.
> How could I use pdb to debug this?
>
> $ python udp_local2.py server
> =A0 File "udp_local2.py", line 36
> =A0 =A0 except:
> =A0 =A0 =A0 =A0 =A0^
> SyntaxError: invalid syntax
>
>
> #!/usr/bin/env python
>
> import random, socket, sys
> s =3D socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
>
> MAX =3D 65535
> PORT =3D 1060
>
> if 2 <=3D len(sys.argv) <=3D 3 and sys.argv[1] =3D=3D 'serve= r':
> =A0 =A0 interface =3D sys.argv[2] if len(sys.argv) > 2 else '&#= 39;
> =A0 =A0 s.bind((interface, PORT))
> =A0 =A0 print 'Listening at', s.getsockname()
> =A0 =A0 while True:
> =A0 =A0 =A0 =A0 data, address =3D s.recvfrom(MAX)
> =A0 =A0 =A0 =A0 if random.randint(0, 1):
> =A0 =A0 =A0 =A0 =A0 =A0 print 'The client at', address, 's= ays:', repr(data)
> =A0 =A0 =A0 =A0 =A0 =A0 s.sendto('Your data was %d bytes' % le= n(data), address)
> =A0 =A0 =A0 =A0 else:
> =A0 =A0 =A0 =A0 =A0 =A0 print 'Pretending to drop packet from'= , address
>
> elif len(sys.argv) =3D=3D 3 and sys.argv[1] =3D=3D 'client': > =A0 =A0 hostname =3D sys.argv[2]
> =A0 =A0 s.connect((hostname, PORT))
> =A0 =A0 print 'Client socket name is', s.getsockname()
> =A0 =A0 delay =3D 0.1
> =A0 =A0 while True:
> =A0 =A0 =A0 =A0 s.send('This is another message')
> =A0 =A0 =A0 =A0 print 'Waiting up to', delay, 'seconds for= a reply'
> =A0 =A0 =A0 =A0 s.settimeout(delay)
> =A0 =A0 =A0 =A0 try:
> =A0 =A0 =A0 =A0 =A0 =A0 data =3D s.recv(MAX)
> =A0 =A0 =A0 =A0 except socket.timeout:
> =A0 =A0 =A0 =A0 =A0 =A0 delay *=3D 2
> =A0 =A0 =A0 =A0 =A0 =A0 if delay > 2.0:
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 raise RuntimeError('I think the se= rver is down')
> =A0 =A0 =A0 =A0 =A0 =A0 except:
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 raise
> =A0 =A0 =A0 =A0 =A0 =A0 else:
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 break
> =A0 =A0 =A0 =A0 print 'The server says', repr(data)
> =A0 =A0 else:
> =A0 =A0 =A0 =A0 print >> sys.stderr, 'usage: %d server [<= interfae>]' %
> =A0 =A0 =A0 =A0 sys.argv[0]
> =A0 =A0 =A0 =A0 print >> sys.stderr, ' =A0 or: %d client <= ;host>' % sys.argv[0]
> =A0 =A0 =A0 =A0 sys.exit(2)
> --
> http://mail.python.org/mailman/listinfo/python-list

Your "except" statement is on the same level as "if delay &g= t; 2.0"..
effectively:

if delay > 2.0
=A0 ...
except:

which is not valid syntax. =A0Excepts go with try: blocks, not if: blocks.<= br> --
http://mail.python.org/mailman/listinfo/python-list

--20cf300fb4b3d1088004d4f4a420--