Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Shaunak Bangale Newsgroups: comp.lang.python Subject: Re: Exception handling for socket.error in Python 3.5/RStudio Date: Fri, 5 Feb 2016 13:50:58 -0700 Lines: 135 Message-ID: References: <1067e50c-bcbd-464e-8013-754bd66fda91@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de rCDIEXzVvXXfwd0bqvc2aAKoGvXNvfC0Sa/Bf34+aTeA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'context': 0.05; 'error:': 0.05; 'chunk': 0.07; 'socket': 0.07; 'versions.': 0.07; 'cc:addr:python-list': 0.09; 'complaining': 0.09; 'server:': 0.09; 'socket.error': 0.09; 'sockets': 0.09; 'underlying': 0.09; 'python': 0.10; 'skip:# 20': 0.13; 'syntax': 0.13; 'suggest': 0.15; 'interpreter': 0.15; '"while': 0.16; '#create': 0.16; '0):': 0.16; '2016': 0.16; 'luck,': 0.16; 'points:': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'ssl_sock': 0.16; 'subject:handling': 0.16; 'syntaxerror:': 0.16; 'to:addr:martin': 0.16; 'wrote:': 0.16; 'skip:l 30': 0.18; 'ssl': 0.18; '>': 0.18; 'input': 0.18; 'variable': 0.18; 'load': 0.20; 'settings': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; '(the': 0.22; 'martin': 0.22; 'assuming': 0.22; 'file:': 0.22; 'fixing': 0.22; 'parse': 0.22; 'skip:= 20': 0.22; 'cc:no real name:2**0': 0.22; 'feb': 0.23; '(you': 0.23; 'bigger': 0.23; 'tried': 0.24; 'header:In-Reply-To:1': 0.24; 'script': 0.25; '----------': 0.25; 'command': 0.26; 'installed': 0.26; 'error': 0.27; 'fri,': 0.27; 'separate': 0.27; 'message- id:@mail.gmail.com': 0.27; 'executing': 0.27; 'locations': 0.27; 'about.': 0.29; 'if,': 0.29; 'print': 0.30; 'subject:/': 0.30; 'comments': 0.30; 'code': 0.30; 'putting': 0.30; 'error.': 0.31; 'skip:s 30': 0.31; 'are:': 0.32; 'older': 0.32; 'getting': 0.33; 'errors,': 0.33; 'traceback': 0.33; 'changing': 0.34; 'recommended': 0.34; 'file': 0.34; 'except': 0.34; 'running': 0.34; 'list': 0.34; 'skip:& 20': 0.35; 'received:google.com': 0.35; 'so,': 0.35; 'could': 0.35; 'saved': 0.35; 'skip:> 10': 0.35; 'reply.': 0.35; 'skip:p 30': 0.35; 'but': 0.36; 'there': 0.36; 'lines': 0.36; 'received:209.85': 0.36; '(and': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'client': 0.37; 'thanks': 0.37; 'missing': 0.37; 'received:209.85.213': 0.37; 'skip:s 50': 0.37; 'associated': 0.38; 'skip:v 20': 0.38; 'received:209': 0.38; 'skip:s 40': 0.38; 'skip:p 20': 0.38; 'end': 0.39; 'sure': 0.39; 'system.': 0.39; 'still': 0.40; 'your': 0.60; 'close': 0.61; 'press': 0.61; 'information,': 0.61; 'customized': 0.61; 'default': 0.61; 'here.': 0.62; 'above,': 0.63; 'more': 0.63; 'here:': 0.63; 'limit': 0.65; 'elsewhere': 0.66; 'skip:\xc2 10': 0.67; '8bit%:21': 0.70; 'saving': 0.70; 'study': 0.70; 'carefully': 0.72; 'explanations': 0.72; 'saw': 0.77; 'desired?': 0.84; 'follow,': 0.84; 'martin,': 0.84; 'probable': 0.84; 'careful': 0.91; 'certificates': 0.91; 'continue.': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=IMG+s8su8G212p8gPmYu61PILNkbCPo7vT13VTc+g8M=; b=Sz4nqjDZ3MHC8KGjPq5YmFbkGJWQ5wZpWQWpCmz7yl/ABJ5gYJ23JJPp6ZRHGYvaa0 4z09eJ5U/2dCUuNAPSkNN4/yThV6MnEOM8UqUziyYVDuKGP9FBRI7J68vH1Xjny/6yoa QODIupshizB2QEw4LsOwhX31WMha9g/jPEMFV3FxfjweWLGYZE9qe/cletzR7A7MF27o 45gSg0BmMpwOmBQ+TVT3stHmF9e/Df+nODw7O8IzEjZ/ydFuG2D610M/b/Qs1NkunOcU D5gH6TcjWAdMl9zrHd5b5PEbqrTipaKd5GNhG6XXiwi1gqrb39CRcLoP6wDiX2ZpGzNJ MINg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; bh=IMG+s8su8G212p8gPmYu61PILNkbCPo7vT13VTc+g8M=; b=KvnNVMf+uT0Ozc8fX0PHykcHsqGDg7douYKE6wrEdO4h6BPXWSijsHOQPVURSkeWtV GLP81suzw1xNxng8rbWWAp/GP3hhDMA2mjMqt3rvT1HJOC4/pdf+tL+Bxa1jdRBEY1/u rsyR+/J2dbKAxSjThivAlqE7Rom2Ayz5YlHlq6QGy0Jp8NMFyn7jgHMV9tdZIROIYb+U 5pMGIss+WNZrx1PHsdAm04Z7vtW4BQwR7e0IRQpWVq1wJCJ5sGMoKqw0KFHA26wFGqtW OKFZIfZ9niIIxcSCSUktXCOAGU+109lTe0Q4XA3cKvLqjLkiZ3YZODJCwptoc5ss2PGK EmuA== X-Gm-Message-State: AG10YORfSWUWWUJsbwcKU2DH2w7qTL7BmBI4xUaoovhpW3b/J2nX8rL93UDEjyyyUtzYSG/ztzCoW0w29w6RfQ== X-Received: by 10.50.61.209 with SMTP id s17mr12101001igr.40.1454705488065; Fri, 05 Feb 2016 12:51:28 -0800 (PST) In-Reply-To: X-Mailman-Approved-At: Sat, 06 Feb 2016 05:37:24 -0500 X-Content-Filtered-By: Mailman/MimeDel 2.1.21rc2 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21rc2 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:102577 Hi Martin, Thanks for the detailed reply. I edited, saved and opened the file again. Still I am getting exactly the same error. Putting bigger chunk of code and the error again: # create socket s = socket.socket(socket.AF_INET) #create a SSL context with the recommended security settings for client sockets, including automatic certificate verification: context = ssl.create_default_context() # Alternatively, a customized context could be created: #context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) #context.verify_mode = ssl.CERT_REQUIRED #context.check_hostname = True # Load a set of default CA certificates from default locations #context.load_default_certs() ssl_sock = context.wrap_socket(s, server_hostname ='firehose.flightaware.com ') print("Connecting...") ssl_sock.connect(('firehose.flightaware.com', 1501)) print("Connection succeeded") # send initialization command to server: ssl_sock.write(bytes(initiation_command, 'UTF-8')) # return a file object associated with the socket file = ssl_sock.makefile('r') # use "while True" for no limit in messages received count = 10 while (count > 0): try : # read line from file: print(file.readline()) # parse parse_json(file.readline()) count = count - 1 except socket.error as e: print('Connection fail', e) print(traceback.format_exc()) # wait for user input to end # input("\n Press Enter to exit..."); # close the SSLSocket, will also close the underlying socket ssl_sock.close() ---------- Error: except socket.error as e: ^ SyntaxError: invalid syntax TIA. On Fri, Feb 5, 2016 at 1:44 PM, Martin A. Brown wrote: > > Hi there Shaunak, > > I saw your few replies to my (and Nathan's) quick identification of > syntax error. More comments follow, here. > > >I am running this python script on R-studio. I have Python 3.5 installed > on my system. > > > >count = 10 > >while (count > 0): > > try : > > # read line from file: > > print(file.readline()) > > # parse > > parse_json(file.readline()) > > count = count - 1 > > except socket.error as e > > print('Connection fail', e) > > print(traceback.format_exc()) > > > ># wait for user input to end > ># input("\n Press Enter to exit..."); > ># close the SSLSocket, will also close the underlying socket > >ssl_sock.close() > > > >The error I am getting is here: > > > >line 53 except socket.error as e ^ SyntaxError: invalid syntax > > > >I tried changing socket.error to ConnectionRefusedError. and still got > the same error. > > We were assuming that line 53 in your file is the part you pasted > above. That clearly shows a syntax error (the missing colon). > > If, after fixing that error, you are still seeing errors, then the > probable explanations are: > > * you are not executing the same file you are editing > > * there is a separate syntax error elsewhere in the file (you sent > us only a fragment) > > Additional points: > > * While the word 'file' is not reserved in Python 3.x, it is in > Python 2.x, so, just be careful when working with older Python > versions. You could always change your variable name, but you > do not need to. > > * When you catch the error in the above, you print the traceback > information, but your loop will continue. Is that what you > desired? > > I might suggest saving your work carefully and make sure that you > are running the same code that you are working on. Then, if you > are still experiencing syntax errors, study the lines that the > interpreter is complaining about. And, of course, send the list an > email. > > Best of luck, > > -Martin > > -- > Martin A. Brown > http://linux-ip.net/ >