Exception handling for socket.error in Python 3.5/RStudio

Shaunak Bangale shaunak.bangale at gmail.com
Fri Feb 5 16:00:26 EST 2016


Hi Martin,

Answering your questions below:


On Fri, Feb 5, 2016 at 1:50 PM, Shaunak Bangale <shaunak.bangale at gmail.com>
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 <martin at linux-ip.net>
> 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/
>>
>
>



More information about the Python-list mailing list