Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #102548 > unrolled thread

Exception handling for socket.error in Python 3.5/RStudio

Started byshaunak.bangale@gmail.com
First post2016-02-05 11:58 -0800
Last post2016-02-19 10:07 -0800
Articles 20 on this page of 22 — 7 participants

Back to article view | Back to comp.lang.python


Contents

  Exception handling for socket.error in Python 3.5/RStudio shaunak.bangale@gmail.com - 2016-02-05 11:58 -0800
    Re: Exception handling for socket.error in Python 3.5/RStudio Nathan Hilterbrand <nhilterbrand@gmail.com> - 2016-02-05 15:07 -0500
      Re: Exception handling for socket.error in Python 3.5/RStudio shaunak.bangale@gmail.com - 2016-02-05 12:11 -0800
        Re: Exception handling for socket.error in Python 3.5/RStudio shaunak.bangale@gmail.com - 2016-02-05 12:17 -0800
    Re: Exception handling for socket.error in Python 3.5/RStudio "Martin A. Brown" <martin@linux-ip.net> - 2016-02-05 12:08 -0800
      Re: Exception handling for socket.error in Python 3.5/RStudio shaunak.bangale@gmail.com - 2016-02-05 12:22 -0800
    Re: Exception handling for socket.error in Python 3.5/RStudio "Martin A. Brown" <martin@linux-ip.net> - 2016-02-05 12:44 -0800
    Re: Exception handling for socket.error in Python 3.5/RStudio "Martin A. Brown" <martin@linux-ip.net> - 2016-02-05 13:01 -0800
    Re: Exception handling for socket.error in Python 3.5/RStudio Chris Angelico <rosuav@gmail.com> - 2016-02-06 08:01 +1100
    Re: Exception handling for socket.error in Python 3.5/RStudio Bernardo Sulzbach <mafagafogigante@gmail.com> - 2016-02-05 19:08 -0200
      Re: Exception handling for socket.error in Python 3.5/RStudio shaunak.bangale@gmail.com - 2016-02-05 13:26 -0800
        Re: Exception handling for socket.error in Python 3.5/RStudio Bernardo Sulzbach <mafagafogigante@gmail.com> - 2016-02-05 19:37 -0200
    Re: Exception handling for socket.error in Python 3.5/RStudio shaunak.bangale@gmail.com - 2016-02-05 13:57 -0800
    Re: Exception handling for socket.error in Python 3.5/RStudio Chris Angelico <rosuav@gmail.com> - 2016-02-06 08:57 +1100
    Re: Exception handling for socket.error in Python 3.5/RStudio shaunak.bangale@gmail.com - 2016-02-05 14:35 -0800
      Re: Exception handling for socket.error in Python 3.5/RStudio Chris Angelico <rosuav@gmail.com> - 2016-02-06 13:04 +1100
    Re: Exception handling for socket.error in Python 3.5/RStudio Shaunak Bangale <shaunak.bangale@gmail.com> - 2016-02-05 13:50 -0700
    Re: Exception handling for socket.error in Python 3.5/RStudio Shaunak Bangale <shaunak.bangale@gmail.com> - 2016-02-05 14:00 -0700
    Re: Exception handling for socket.error in Python 3.5/RStudio Shaunak Bangale <shaunak.bangale@gmail.com> - 2016-02-05 14:15 -0700
    Re: Exception handling for socket.error in Python 3.5/RStudio dieter <dieter@handshake.de> - 2016-02-07 09:23 +0100
      Re: Exception handling for socket.error in Python 3.5/RStudio shaunak.bangale@gmail.com - 2016-02-08 00:04 -0800
        Re: Exception handling for socket.error in Python 3.5/RStudio shaunak.bangale@gmail.com - 2016-02-19 10:07 -0800

Page 1 of 2  [1] 2  Next page →


#102548 — Exception handling for socket.error in Python 3.5/RStudio

Fromshaunak.bangale@gmail.com
Date2016-02-05 11:58 -0800
SubjectException handling for socket.error in Python 3.5/RStudio
Message-ID<1067e50c-bcbd-464e-8013-754bd66fda91@googlegroups.com>
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

[toc] | [next] | [standalone]


#102549

FromNathan Hilterbrand <nhilterbrand@gmail.com>
Date2016-02-05 15:07 -0500
Message-ID<mailman.14.1454702872.2317.python-list@python.org>
In reply to#102548
On Feb 5, 2016 15:01, <shaunak.bangale@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
> --
> 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

[toc] | [prev] | [next] | [standalone]


#102551

Fromshaunak.bangale@gmail.com
Date2016-02-05 12:11 -0800
Message-ID<824579b6-b0c5-4447-a123-3fc2b850369f@googlegroups.com>
In reply to#102549
On Friday, February 5, 2016 at 1:08:11 PM UTC-7, Nathan Hilterbrand wrote:
> On Feb 5, 2016 15:01, <shaunak.bangale@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
> > --
> > 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.

[toc] | [prev] | [next] | [standalone]


#102552

Fromshaunak.bangale@gmail.com
Date2016-02-05 12:17 -0800
Message-ID<0f14bba2-1dbb-4c41-beda-992939b1a968@googlegroups.com>
In reply to#102551
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, <shaunak.bangale@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
> > > --
> > > 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())

[toc] | [prev] | [next] | [standalone]


#102550

From"Martin A. Brown" <martin@linux-ip.net>
Date2016-02-05 12:08 -0800
Message-ID<mailman.15.1454702950.2317.python-list@python.org>
In reply to#102548
>    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/

[toc] | [prev] | [next] | [standalone]


#102553

Fromshaunak.bangale@gmail.com
Date2016-02-05 12:22 -0800
Message-ID<6e83a175-83e8-4759-88c1-076339c54d0a@googlegroups.com>
In reply to#102550
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.

[toc] | [prev] | [next] | [standalone]


#102554

From"Martin A. Brown" <martin@linux-ip.net>
Date2016-02-05 12:44 -0800
Message-ID<mailman.16.1454705060.2317.python-list@python.org>
In reply to#102548
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/

[toc] | [prev] | [next] | [standalone]


#102555

From"Martin A. Brown" <martin@linux-ip.net>
Date2016-02-05 13:01 -0800
Message-ID<mailman.18.1454706087.2317.python-list@python.org>
In reply to#102548
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 <module>
      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/

[toc] | [prev] | [next] | [standalone]


#102556

FromChris Angelico <rosuav@gmail.com>
Date2016-02-06 08:01 +1100
Message-ID<mailman.17.1454706087.2317.python-list@python.org>
In reply to#102548
On Sat, Feb 6, 2016 at 6:58 AM,  <shaunak.bangale@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#102557

FromBernardo Sulzbach <mafagafogigante@gmail.com>
Date2016-02-05 19:08 -0200
Message-ID<mailman.19.1454706532.2317.python-list@python.org>
In reply to#102548
On 02/05/2016 07:01 PM, Chris Angelico wrote:
> On Sat, Feb 6, 2016 at 6:58 AM,  <shaunak.bangale@gmail.com> 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.

[toc] | [prev] | [next] | [standalone]


#102560

Fromshaunak.bangale@gmail.com
Date2016-02-05 13:26 -0800
Message-ID<f18c9de8-c575-4025-b6a9-93254d0b990f@googlegroups.com>
In reply to#102557
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,  <shaunak.bangale@gmail.com> 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,

[toc] | [prev] | [next] | [standalone]


#102561

FromBernardo Sulzbach <mafagafogigante@gmail.com>
Date2016-02-05 19:37 -0200
Message-ID<mailman.21.1454708251.2317.python-list@python.org>
In reply to#102560
On 02/05/2016 07:26 PM, shaunak.bangale@gmail.com wrote:
>
>     from _ssl import RAND_status, RAND_egd, RAND_add
> ImportError: cannot import name 'RAND_egd'
>

I believe I've already seen this issue myself. It has to do with 
LibreSSL not having RAND_egd for some reason I can't recall.

This seems to be the related ticket https://bugs.python.org/issue21356

On 02/05/2016 07:26 PM, shaunak.bangale@gmail.com wrote:
 > 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.

Are you sure that the Python you talk about is 3.5?

[toc] | [prev] | [next] | [standalone]


#102564

Fromshaunak.bangale@gmail.com
Date2016-02-05 13:57 -0800
Message-ID<af4b1f00-e482-4685-9da2-4ddc55ea271b@googlegroups.com>
In reply to#102548
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.

[toc] | [prev] | [next] | [standalone]


#102565

FromChris Angelico <rosuav@gmail.com>
Date2016-02-06 08:57 +1100
Message-ID<mailman.24.1454709482.2317.python-list@python.org>
In reply to#102548
On Sat, Feb 6, 2016 at 8:08 AM, Bernardo Sulzbach
<mafagafogigante@gmail.com> wrote:
> On 02/05/2016 07:01 PM, Chris Angelico wrote:
>>
>> On Sat, Feb 6, 2016 at 6:58 AM,  <shaunak.bangale@gmail.com> 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.

Stuff might work, but if you think you're using 3.5 and you're
actually running under 3.4, there's a high probability that you have
two Python installations and it's picking the wrong one. That, in
turn, means problems with package installations and such, so it's a
reasonable smoke test to try. (It's unlikely there'll be two different
3.5s installed.) And, of course, if it shows up that it's running
under *2*.5, well, that would explain a lot :)

Anyway, it is indeed what's expected - smoke test passed.

ChrisA

[toc] | [prev] | [next] | [standalone]


#102567

Fromshaunak.bangale@gmail.com
Date2016-02-05 14:35 -0800
Message-ID<e05d99b4-fbcf-40c8-8a25-e824d55ed912@googlegroups.com>
In reply to#102548
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

[toc] | [prev] | [next] | [standalone]


#102570

FromChris Angelico <rosuav@gmail.com>
Date2016-02-06 13:04 +1100
Message-ID<mailman.28.1454724261.2317.python-list@python.org>
In reply to#102567
On Sat, Feb 6, 2016 at 9:35 AM,  <shaunak.bangale@gmail.com> wrote:
> 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?

(Please quote the person you're replying to, rather than simply
quoting your own original post - it helps to provide context. Thanks!)

The next thing to try would be to add the colon at the end of the
"except" clause, as has already been suggested, and then to post the
latest code (preferably all of it - if it looks like too much, it's
worth shortening the code, rather than posting a snippet) and the full
traceback.

ChrisA

[toc] | [prev] | [next] | [standalone]


#102577

FromShaunak Bangale <shaunak.bangale@gmail.com>
Date2016-02-05 13:50 -0700
Message-ID<mailman.34.1454755045.2317.python-list@python.org>
In reply to#102548
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@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.
>
>   * 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/
>

[toc] | [prev] | [next] | [standalone]


#102578

FromShaunak Bangale <shaunak.bangale@gmail.com>
Date2016-02-05 14:00 -0700
Message-ID<mailman.35.1454755046.2317.python-list@python.org>
In reply to#102548
Hi Martin,

Answering your questions below:


On Fri, Feb 5, 2016 at 1:50 PM, Shaunak Bangale <shaunak.bangale@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@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/
>>
>
>

[toc] | [prev] | [next] | [standalone]


#102579

FromShaunak Bangale <shaunak.bangale@gmail.com>
Date2016-02-05 14:15 -0700
Message-ID<mailman.36.1454755046.2317.python-list@python.org>
In reply to#102548
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 <martin@linux-ip.net> 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 <module>
>       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/
>
>

[toc] | [prev] | [next] | [standalone]


#102618

Fromdieter <dieter@handshake.de>
Date2016-02-07 09:23 +0100
Message-ID<mailman.64.1454833396.2317.python-list@python.org>
In reply to#102548
Shaunak Bangale <shaunak.bangale@gmail.com> 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.

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.python


csiph-web