Exception handling for socket.error in Python 3.5/RStudio

2016-02-05 Thread shaunak . bangale
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.

Please tell me if the problem is with Rstudio, Python version or the syntax.

TIA
-Shaunak
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Exception handling for socket.error in Python 3.5/RStudio

2016-02-05 Thread shaunak . bangale
On Friday, February 5, 2016 at 1:08:11 PM UTC-7, Nathan Hilterbrand wrote:
> On Feb 5, 2016 15:01,  wrote:
> >
> > 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.
> >
> > Please tell me if the problem is with Rstudio, Python version or the
> syntax.
> >
> > TIA
> > -Shaunak
> > --
> > https://mail.python.org/mailman/listinfo/python-list
> 
> Looks like you are missing a colon after 'as e'... as e:  might do
> the trick
> 
> Nathan

Hi Nathan,

Tried colon and a comma as well. Both did not work.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Exception handling for socket.error in Python 3.5/RStudio

2016-02-05 Thread shaunak . bangale
On Friday, February 5, 2016 at 1:11:19 PM UTC-7, shaunak...@gmail.com wrote:
> On Friday, February 5, 2016 at 1:08:11 PM UTC-7, Nathan Hilterbrand wrote:
> > On Feb 5, 2016 15:01,  wrote:
> > >
> > > 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.
> > >
> > > Please tell me if the problem is with Rstudio, Python version or the
> > syntax.
> > >
> > > TIA
> > > -Shaunak
> > > --
> > > https://mail.python.org/mailman/listinfo/python-list
> > 
> > Looks like you are missing a colon after 'as e'... as e:  might do
> > the trick
> > 
> > Nathan
> 
> Hi Nathan,
> 
> Tried colon and a comma as well. Both did not work.

Of course I am new to Python. I am wondering if it has to do anything with the 
indentation. Putting my most recent code here, again:

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())
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Exception handling for socket.error in Python 3.5/RStudio

2016-02-05 Thread shaunak . bangale
On Friday, February 5, 2016 at 1:09:35 PM UTC-7, Martin A. Brown wrote:
> >except socket.error as e
> 
> >line 53 except socket.error as e ^ SyntaxError: invalid syntax
> >
> >I tried changing socket.error to ConnectionRefusedError. and still 
> >got the same error.
> 
> >Please tell me if the problem is with Rstudio, Python version or 
> >the syntax.
> 
> Syntax.
> 
> Your code has, unfortunately, suffered a colonectomy.
> 
> When you transplant a colon, it is more likely to function properly 
> again.  For example:
> 
>except socket.error as e:
> 
> Good luck,
> 
> -Martin
> 
> -- 
> Martin A. Brown
> http://linux-ip.net/

I was first running with a colon only. Later tried with a comma. But it didn't 
work. I got the same error.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Exception handling for socket.error in Python 3.5/RStudio

2016-02-05 Thread shaunak . bangale
On Friday, February 5, 2016 at 2:09:11 PM UTC-7, Bernardo Sulzbach wrote:
> On 02/05/2016 07:01 PM, Chris Angelico wrote:
> > On Sat, Feb 6, 2016 at 6:58 AM,   wrote:
> >> I am running this python script on R-studio. I have Python 3.5 installed 
> >> on my system.
> >>
> >
> > Let's just try a quick smoke test. Run this script:
> >
> > import sys
> > print(sys.version)
> > input("Press Enter to exit...")
> >
> > That'll tell you a few things about how your system is set up. Most
> > notably, if it doesn't say you're using Python 3.5, there's a problem.
> >
> > ChrisA
> >
> 
> Is there? If he just got the minor version wrong it wouldn't be a 
> problem. Unless RStudio requires 3.**5** for some reason.

Hi Chris,
Output:

3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 02:16:59) [MSC v.1900 32 bit (Intel)]

Hi Martin,

I do have the initiation command defined. Just that I am not allowed to make 
the username, pwd public.

I am absolutely sure I am running the same code. Now opened the same file with 
Python 3.5 shell and I get following error:

   from _ssl import RAND_status, RAND_egd, RAND_add
ImportError: cannot import name 'RAND_egd'

I am new to coding and this code has been borrowed from an online source but I 
can see same code working on mac+Rstudio+python combo.

Salute your patience.

Sincerely,
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Exception handling for socket.error in Python 3.5/RStudio

2016-02-05 Thread shaunak . bangale
On Friday, February 5, 2016 at 12:58:37 PM UTC-7, shaunak...@gmail.com wrote:
> 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.
> 
> Please tell me if the problem is with Rstudio, Python version or the syntax.
> 
> TIA
> -Shaunak

It is 3.5. I am looking at the link.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Exception handling for socket.error in Python 3.5/RStudio

2016-02-05 Thread shaunak . bangale
On Friday, February 5, 2016 at 12:58:37 PM UTC-7, shaunak...@gmail.com wrote:
> 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.
> 
> Please tell me if the problem is with Rstudio, Python version or the syntax.
> 
> TIA
> -Shaunak


Chris,

That sounds legitimate.
But I never installed second 3.X version and deleted previous 2.X version and 
using only 3.5 now. What will be the next test according to you?

-Shaun
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Exception handling for socket.error in Python 3.5/RStudio

2016-02-06 Thread Shaunak Bangale
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/
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Exception handling for socket.error in Python 3.5/RStudio

2016-02-06 Thread Shaunak Bangale
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/
>>
>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Exception handling for socket.error in Python 3.5/RStudio

2016-02-06 Thread Shaunak Bangale
I do have the initiation command defined. Just that I am not allowed to
make the username, pwd public.

I am absolutely sure I am running the same code. Now opened the same file
with Python 3.5 shell and I get following error:

   from _ssl import RAND_status, RAND_egd, RAND_add
ImportError: cannot import name 'RAND_egd'

I am new to coding and this code has been borrowed from an online source
but I can see same code working on mac+Rstudio+python combo.

Salute your patience.

Sincerely,

On Fri, Feb 5, 2016 at 2:01 PM, Martin A. Brown  wrote:

>
> Hi there,
>
> >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:
>
> [snipped; thanks for the larger chunk]
>
> >Error:
> >except socket.error as e:
> > ^
> >SyntaxError: invalid syntax
>
> I ran your code.  I see this:
>
>   $ python3 shaunak.bangale.py
>   Connecting...
>   Connection succeeded
>   Traceback (most recent call last):
> File "shaunak.bangale.py", line 23, in 
>   ssl_sock.write(bytes(initiation_command, 'UTF-8'))
>   NameError: name 'initiation_command' is not defined
>
> Strictly speaking, I don't think you are having a Python problem.
>
>   * Are you absolutely certain you are (or your IDE is) executing
> the same code you are writing?
>
>   * How would you be able to tell?  Close your IDE.  Run the code on
> the command-line.
>
>   * How much time have you taken to work out what the interpreter is
> telling you?
>
> Good luck,
>
> -Martin
>
> --
> Martin A. Brown
> http://linux-ip.net/
>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Exception handling for socket.error in Python 3.5/RStudio

2016-02-08 Thread shaunak . bangale
On Sunday, February 7, 2016 at 1:23:32 AM UTC-7, dieter wrote:
> Shaunak Bangale  writes:
> > ...
> > 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())
> > ...
> > Error:
> > except socket.error as e:
> >  ^
> > SyntaxError: invalid syntax
> 
> Are you sure, that there is no invisible character at the end
> of that line?
> 
> When I try code like the above (in PYthon 2.7), there is no
> "SyntaxError":
> 
> >>> while(False):
> ... 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())
> ... 
> >>> 
> 
> 
> Another reason for your problem could be an older Python version.
> The "as" construct as part of the "except" clause is a more recent
> addition to Python.

Hi Dieter,

I typed that line again but didn't help. I am using Python 3.5.
-- 
https://mail.python.org/mailman/listinfo/python-list


ImportError: cannot import name 'RAND_egd'

2016-02-09 Thread shaunak . bangale
Hi,

I am trying to run a 60 lines Python code which is running on a mac machine but 
on windows machine, I am getting this error when I run on it on shell(open file 
and run module). I have Python 3.5 installed.

   from _ssl import RAND_status, RAND_egd, RAND_add
ImportError: cannot import name 'RAND_egd'

Form forums, I found that it is a common error but could not find a good 
solution that will work for me.

One of the ways was to create scripts folder and putting easy_install.exe and 
then running easy_install pip but that gave me sytnax error.

Please advise. Thanks in advance.

Regards
Shaunak
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: ImportError: cannot import name 'RAND_egd'

2016-02-10 Thread shaunak . bangale
On Tuesday, February 9, 2016 at 1:33:23 PM UTC-7, Ian wrote:
> On Tue, Feb 9, 2016 at 7:55 AM,   wrote:
> > Hi,
> >
> > I am trying to run a 60 lines Python code which is running on a mac machine 
> > but on windows machine, I am getting this error when I run on it on 
> > shell(open file and run module). I have Python 3.5 installed.
> >
> >from _ssl import RAND_status, RAND_egd, RAND_add
> > ImportError: cannot import name 'RAND_egd'
> 
> Why are you importing these directly from the "_ssl" C module and not
> from the "ssl" wrapper module? Anything that starts with an _ should
> be considered a private implementation detail and shouldn't be relied
> upon.
> 
> > Form forums, I found that it is a common error but could not find a good 
> > solution that will work for me.
> >
> > One of the ways was to create scripts folder and putting easy_install.exe 
> > and then running easy_install pip but that gave me sytnax error.
> >
> > Please advise. Thanks in advance.
> 
> The ssl module in the standard library has this:
> 
> try:
> from _ssl import RAND_egd
> except ImportError:
> # LibreSSL does not provide RAND_egd
> pass
> 
> So it looks like you cannot depend on ssl.RAND_egd to be present.

Hi Ian,
Thanks for your reply.
I wasn't trying to import it from _ssl. That was part of the error. My code did 
not have RAND_egd. I think it was just about ssl package being missing. After 
installing analcondra distribution, it stopped throwing this particular error 
at least.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Exception handling for socket.error in Python 3.5/RStudio

2016-02-19 Thread shaunak . bangale
On Monday, February 8, 2016 at 1:05:08 AM UTC-7, shaunak...@gmail.com wrote:
> On Sunday, February 7, 2016 at 1:23:32 AM UTC-7, dieter wrote:
> > Shaunak Bangale  writes:
> > > ...
> > > 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())
> > > ...
> > > Error:
> > > except socket.error as e:
> > >  ^
> > > SyntaxError: invalid syntax
> > 
> > Are you sure, that there is no invisible character at the end
> > of that line?
> > 
> > When I try code like the above (in PYthon 2.7), there is no
> > "SyntaxError":
> > 
> > >>> while(False):
> > ... 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())
> > ... 
> > >>> 
> > 
> > 
> > Another reason for your problem could be an older Python version.
> > The "as" construct as part of the "except" clause is a more recent
> > addition to Python.
> 
> Hi Dieter,
> 
> I typed that line again but didn't help. I am using Python 3.5.
-

Hi people, it finally worked. One problem was with the way modules were set up, 
so installed anacoda and using accessing it through PyCharm. It's working well 
now. Another problem was with the initiation command, had to renew the password 
and that's working too. I sincerely thank everyone here who tried to help me. 
:) 
Have a great weekend guys!

-Shaun
-- 
https://mail.python.org/mailman/listinfo/python-list