Path: csiph.com!news.redatomik.org!news.freedyn.net!newsreader4.netcologne.de!news.netcologne.de!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 14:00:26 -0700 Lines: 158 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 Eksrexh+91K8axUkzuvWFQGeX3ZiIOBNfkiA8nVRprXA== 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; 'answering': 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; 'email addr:gmail.com>': 0.18; 'input': 0.18; 'variable': 0.18; 'load': 0.20; 'settings': 0.20; 'windows': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; '(the': 0.22; 'martin': 0.22; '3.x': 0.22; 'assuming': 0.22; 'file:': 0.22; 'fixing': 0.22; 'parse': 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; 'supposed': 0.31; 'skip:s 30': 0.31; 'are:': 0.32; 'older': 0.32; 'getting': 0.33; 'run': 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; 'according': 0.36; 'but': 0.36; 'there': 0.36; 'lines': 0.36; 'received:209.85': 0.36; '(and': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'client': 0.37; 'thanks': 0.37; 'mac': 0.37; 'missing': 0.37; 'received:209.85.213': 0.37; 'skip:s 50': 0.37; 'associated': 0.38; 'skip:v 20': 0.38; 'times.': 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; 'questions': 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; 'below:': 0.71; 'carefully': 0.72; 'explanations': 0.72; 'hoping': 0.77; '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=YuHvDkKsBazN8ij3LvsDvwbPk+1Z7aEx/P6XXAWhc+0=; b=sLLazGJW2BzNOO7Xt8tHslNvF7CghGkO0df7pKpYdq+fwY5is5txMAH6DcAgh5xdnt 50fNe0niMwHMbPcM5Kp0OjT5TaoitDtc9oEpy69nfByQSlDm7SGqc3cb0YfsoxXngszZ C8vDIL3tMzFlb71DO/+Umc3KBDNJCFzs68L+bDbz3U5r7siv6+6NC/o/i1+dG+EAlYAo Bkb9XNAAWbGSMXHQ6bNMvZsLY2ZjD4qI0wV1KcrAqaBw1mT/0fhWJyhxgSYm0rYidJot Wu786N3L4ycs/SILYDwXxVJD65sj4xVfIpvnZR6QM3KqteBXjQYRBUS/DxguTUH/Zpda r6Yg== 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=YuHvDkKsBazN8ij3LvsDvwbPk+1Z7aEx/P6XXAWhc+0=; b=EiB0RVefRlRh0TU+IEH82f8a8tad/SD1fCKBnWeUzMmTAH7YDYcsT8fUk+tshxK8jz CvJ1NkjEAQ+p2AKE9JnSWsZL8d7M43FyC+Ed9qTzdDsf51E5C5SV1+v0jf8iQ1UqHebW 4QqQFPFRcLicMBUHcv75tTQu1U9MBFlPQSyDBqUaLQcp8F1yHID5Z4eLnexZs5N+ux9Z /oFshN/QXc5wPrWiRfXvQleJgAfX8v3YDfZ9dZ+jav+0BH5tHskKU4JWNvXNfkIie4Y+ ji87ldWhGbJ2ATvylVews5aaHzvC0ChTUDrLLPCkRFHADbVSQDwWxsmuat88T/VdqKSS kG+A== X-Gm-Message-State: AG10YOTy7z5UdMT7zdi/Z4Txk1v9BPE020/vBdPMj+NXHogpm0w5hpnt8yc5y5fVvUn0DjVxZp5Lz8JgIreAUg== X-Received: by 10.50.112.102 with SMTP id ip6mr17816219igb.88.1454706055799; Fri, 05 Feb 2016 13:00:55 -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:102578 Hi Martin, Answering your questions below: On Fri, Feb 5, 2016 at 1:50 PM, Shaunak Bangale wrote: > 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. >> >> But according to FlighAware, this code is supposed to work on the Python 3.X and I have Python 3.5 on my computer and I am hoping the same is being used by Rstudio. > * When you catch the error in the above, you print the traceback >> information, but your loop will continue. Is that what you >> desired? >> >> Yes, I want the loop to run 10 times. > 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. >> >> The same code is supposedly running on a mac machine- Rstudio. I am not sure if the issue is also with Windows 7- Rstudio- Python 3.5 combo. > Best of luck, >> >> -Martin >> >> -- >> Martin A. Brown >> http://linux-ip.net/ >> > >