Re: Simple SSL client hangs

2021-07-14 Thread Loris Bennett
Hi,

MRAB  writes:

> On 2021-07-13 08:50, Loris Bennett wrote:
>> Hi,
>>
>> In Perl I have the following
>>
>>use IO::Socket::SSL;
>>
>>my $my_socket = new IO::Socket::SSL(PeerAddr => 'some.server.somewhere,
>>PeerPort => 12345,
>>   );
>>
>>my $line = <$my_socket>;
>>print("$line\n");
>>say $my_socket 'ECHO 1';
>>$line = <$my_socket>;
>>print("$line\n");
>>
>> This runs as expected and I get
>>
>>200 Some Server Somewhere - Hello [123.456.789.123]
>>
>>310 Hello Echo
>>
>> If I try the same with the following Python code:
>>
>>import socket
>>import ssl
>>
>>HOST = "some.server.somewhere"
>>PORT = 12345
>>
>>context = ssl.create_default_context()
>>
>>with socket.create_connection((HOST, PORT)) as sock:
>>with context.wrap_socket(sock, server_hostname=HOST) as ssock:
>>data = ssock.recv(1024)
>>print(data.decode())
>>ssock.write(b'ECHO 1')
>>data = ssock.read(1024)
>>print(data.decode())
>>
>> I get a timeout for the 'ECHO' command:
>>
>>200 Some Server Somewhere - Hello [123.456.789.123]
>>
>>501 Timeout
>>
>> Does anyone have an idea what I might be doing wrong?
>>
> The docs for Perl says that 'say' appends a newline.
>
> On the other hand, 'ssock.write' doesn't append a newline.
>
> Could that be the problem?

Thanks and well-spotted everyone!  The missing newline was indeed the
problem.  With

  ssock.write(b'ECHO 1\n')

the Python codes behaves like the Perl code.

Cheers,

Loris

-- 
This signature is currently under construction.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Tkinter widgets for desktop database application

2021-07-14 Thread Alan Gauld via Python-list
On 13/07/2021 21:24, Rich Shepard wrote:

> What have other developers used for the UI on a stand-alone database
> application not using a web browser? 

Mostly I just use a scrolledlistbox and a set of functions for
formatting the data into columns for display. The big snag is
you can't do spreadsheet-like things such as select a single
field value or sort by a single column without writing a
fair bit of code. But for simple display, and selection
of a single record it works ok.

But a good grid control would be nice. I did try to use
the Tix.grid widget but failed. And now Tix is deprecated.
I'm sure there must be a third-party one somewhere but
I've never taken the time to hunt one down.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: Tkinter widgets for desktop database application

2021-07-14 Thread Terry Reedy

On 7/13/2021 4:24 PM, Rich Shepard wrote:

I'm writing a couple of database applications that use tkinter, and not a
web browser, for the UI and I'm still trying to determine the optimal 
way to

do this.

Individual tk and ttk widgets (LineEntry, Combobox, etc.) work for adding


Do you mean Entry?

and modifying individual database table rows but not for displaying 
multiple

rows returned by a SELECT statement. To view all rows returned by the SQL
statement would a pythonReport be used?


ttk.Treeview can be used for read-only hierarchical and table data. 
Search 'python ttk Treeview' and Images for examples.




I'm working on learning to use tksheet as the sole widget because it can
display multiple rows from a database table as well as add new rows and
modify existing ones (one or more columns in each row).


I have not used it but have read some of manual and it seems suitable 
for editing flat table.



--
Terry Jan Reedy

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


Re: Tkinter widgets for desktop database application

2021-07-14 Thread Betty Hollinshead
On Tuesday, 13 July 2021 at 21:35:01 UTC+1, Rich Shepard wrote:
> I'm writing a couple of database applications that use tkinter, and not a 
> web browser, for the UI and I'm still trying to determine the optimal way to 
> do this. 
> 
> Individual tk and ttk widgets (LineEntry, Combobox, etc.) work for adding 
> and modifying individual database table rows but not for displaying multiple 
> rows returned by a SELECT statement. To view all rows returned by the SQL 
> statement would a pythonReport be used? 
> 
> I'm working on learning to use tksheet as the sole widget because it can 
> display multiple rows from a database table as well as add new rows and 
> modify existing ones (one or more columns in each row). 
> 
> What have other developers used for the UI on a stand-alone database 
> application not using a web browser? I've struggled with finding the most 
> efficient and optimal non-brower UI for months and would like to have a 
> solid path toward a working solution. I know too little to make this 
> decision and want advice, suggestions, and recommendations from experienced 
> developers. 
> 
> TIA, 
> 
> Rich

So.I've had success with GTK3 (not Tkinter).  Steps involve a non-Pythonic 
start:
1.  Design your GUI with Glade.  It's a GUI design tool for GUIs!  Output is an 
XML definition tool
  which you import into your Python program.
  To your point about multiple lines, see the GTK TextView widget.
2.   In Python:
   from gi.repository import GObject, Gio, Gdk, Gtk
   ...
   builder = Gtk.Builder.new_from_file("mainwin.xml")
   builder.connect_signals(self)
   .
   self.MainWindow1 = builder.get_object("window")
   ..
   ..and the other GUI definitions from Glade/XML
   ...and so on..I've used this basic structure with the 
Python3 interface to SQLITE3 with success.

Sorry about the paucity of the example.it's a big subjectbut there's 
lots of help out there!
L.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Tkinter widgets for desktop database application

2021-07-14 Thread Rich Shepard

On Wed, 14 Jul 2021, Betty Hollinshead wrote:


So.I've had success with GTK3 (not Tkinter).


Betty,

There are multiple widget sets and all will work better with some
application types than others.

In the past I used wxPython but switched to Tkinter. Tried PyQt5 (doesn't do
complex, multi-table queries like it does single table queries) and
seriously considered GTK3. After all that time and effort I decided to stick
with tkinter for two reasons: 1) I know it better than PyQt5 and 2) most
users of one application I'm developing run Microsoft's OSes, some Apple.
Since Tkinter comes with Python3 it's one less thing for non-techical users
to struggle with if they decide to use this application.

Thanks for your suggestion,

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


Re: argparse support of/by argparse

2021-07-14 Thread Dan Stromberg
On Mon, Jul 12, 2021 at 12:34 PM Chris Angelico  wrote:

> On Tue, Jul 13, 2021 at 5:22 AM lucas  wrote:
> > Running CPython on it will raise a TypeError, and running Mypy on it
> > will indicate that no issues were found.
> >
> > I was wondering if there is any way for me to have mypy detecting the
> > args.n type, based on the type keyword of the parser.add_argument
> function ?
> >
> > It appears that some type annotations were added to tierce party
> > modules, provided by mypy itself. Is there a technical issue preventing
> > such work to be made for argparse (or other CLI ; i didn't find anything
> > for others either)
> >
>
> Seems complicated, since it depends on a lot of run-time information.
> What if you flip the problem on its head? Instead of creating the
> argparser and letting that govern the types, maybe create a dataclass,
> and then programmatically build the parser.
>

This is why I eschew argparse.  I instead frequently do something like:

def usage(retval):
"""Output a usage message."""
if retval:
write = sys.stderr.write
else:
write = sys.stdout.write

write(f'{sys.argv[0]} --age 50 --help\n')

sys.exit(retval)


def main():
"""Compute maximum heart rate as a function of age."""
age = -1
while sys.argv[1:]:
if sys.argv[1] == '--age':
age = float(sys.argv[2])
del sys.argv[1]
elif sys.argv[1] in ('-h', '--help'):
usage(0)
else:
sys.stderr.write(f'{sys.argv[0]}: unrecognized option:
{sys.argv[1]}\n')
usage(1)
del sys.argv[1]

if age == -1:
sys.stderr.write('--age is a required option\n')
usage(1)


It is of course a little more typing, but tools like mypy and pylint are
much more effective this way.  This is a small program, but in a large one,
this approach can really help with correctness.

If someone has a mypy extension/plugin that can do argparse as well, I may
switch.  Until then
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: argparse support of/by argparse

2021-07-14 Thread Chris Angelico
On Thu, Jul 15, 2021 at 2:57 PM Dan Stromberg  wrote:
>
>
> On Mon, Jul 12, 2021 at 12:34 PM Chris Angelico  wrote:
>>
>> On Tue, Jul 13, 2021 at 5:22 AM lucas  wrote:
>> > Running CPython on it will raise a TypeError, and running Mypy on it
>> > will indicate that no issues were found.
>> >
>> > I was wondering if there is any way for me to have mypy detecting the
>> > args.n type, based on the type keyword of the parser.add_argument function 
>> > ?
>> >
>> > It appears that some type annotations were added to tierce party
>> > modules, provided by mypy itself. Is there a technical issue preventing
>> > such work to be made for argparse (or other CLI ; i didn't find anything
>> > for others either)
>> >
>>
>> Seems complicated, since it depends on a lot of run-time information.
>> What if you flip the problem on its head? Instead of creating the
>> argparser and letting that govern the types, maybe create a dataclass,
>> and then programmatically build the parser.
>
>
> This is why I eschew argparse.  I instead frequently do something like:
>
> def usage(retval):
> """Output a usage message."""
> if retval:
> write = sys.stderr.write
> else:
> write = sys.stdout.write
>
> write(f'{sys.argv[0]} --age 50 --help\n')
>
> sys.exit(retval)
>
>
> def main():
> """Compute maximum heart rate as a function of age."""
> age = -1
> while sys.argv[1:]:
> if sys.argv[1] == '--age':
> age = float(sys.argv[2])
> del sys.argv[1]
> elif sys.argv[1] in ('-h', '--help'):
> usage(0)
> else:
> sys.stderr.write(f'{sys.argv[0]}: unrecognized option: 
> {sys.argv[1]}\n')
> usage(1)
> del sys.argv[1]
>
> if age == -1:
> sys.stderr.write('--age is a required option\n')
> usage(1)
>
>
> It is of course a little more typing, but tools like mypy and pylint are much 
> more effective this way.  This is a small program, but in a large one, this 
> approach can really help with correctness.
>
> If someone has a mypy extension/plugin that can do argparse as well, I may 
> switch.  Until then
>

The more complicated the program, the more you lose by this approach.
Yes, mypy is satisfied with your code, but your argument parsing is
more rigid and you don't get all the other benefits of argparse. Plus
it's all too easy to make a copy and paste error, where you make a new
parameter that accidentally puts its value into the same variable as
an old parameter, or something.

Much better to make a wrapper around argparse. I've made good use of
clize [1], and have made ad-hoc wrappers for various purposes. Try
adding mypy support to something like that instead - it's likely to be
a lot easier. In fact, since clize primarily uses function arguments,
it'd probably Just Work™ for the most part, although I haven't tried.

[1] https://pypi.org/project/clize/

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