s.sendall(filename + "\r\n") TypeError: a bytes-like object is required, not 'str'

2019-06-19 Thread vakul bhatt
Hi Team,

i m new to python, running below program, getting error
Python Version : 3.7 32bit for windows
Program:

#simple Goopher client

import socket, sys

port = 70
host = sys.argv[1]
filename = sys.argv[2]

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))

s.sendall(filename + "\r\n")

while 1:
  buf = s.recv(2048)
  if not len(buf):
break
  sys.stdout.write(buf)



Error :


python goopher.py quux.org /
Traceback (most recent call last):
  File "goopher.py", line 13, in 
s.sendall(filename + "\r\n")
TypeError: a bytes-like object is required, not 'str'



please help.


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


Re: s.sendall(filename + "\r\n") TypeError: a bytes-like object is required, not 'str'

2019-06-19 Thread Chris Angelico
On Thu, Jun 20, 2019 at 12:31 AM vakul bhatt  wrote:
>
> Hi Team,
>
> i m new to python, running below program, getting error
> Python Version : 3.7 32bit for windows
> Program:
> 
> #simple Goopher client
>
> import socket, sys
>
> port = 70
> host = sys.argv[1]
> filename = sys.argv[2]
>
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> s.connect((host, port))
>
> s.sendall(filename + "\r\n")
>
> while 1:
>   buf = s.recv(2048)
>   if not len(buf):
> break
>   sys.stdout.write(buf)
> 
>
>
> Error :
>
> 
> python goopher.py quux.org /
> Traceback (most recent call last):
>   File "goopher.py", line 13, in 
> s.sendall(filename + "\r\n")
> TypeError: a bytes-like object is required, not 'str'
>

As the message says, you need to have a sequence of bytes, not a text
string. You can't write text to a socket. The way to represent text
using bytes is to use a character encoding such as UTF-8. Try this
instead:

s.sendall(filename.encode("UTF-8") + b"\r\n")

That will send the bytes that make up the UTF-8 representation of the
string, rather than trying to send the abstract characters.

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


tkinter: widget to display set returned from database table

2019-06-19 Thread Rich Shepard

In a database application I want to design a view for the table rows
returned from a select statement. Tkinter has a listbox widget and web
searches suggest that multicolumn listboxes are best based on ttk.Treeview
widgets, but my understanding of a treeview is to display a hierarchical set
rather than a simple list.

Each table has multiple columns and I want to create a view for each table
that will allow the user to select a row. The SQL select statement will be
formed from criteria provided in a separate dialog box.

Advice needed,

Rich

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


Re: tkinter: widget to display set returned from database table

2019-06-19 Thread MRAB

On 2019-06-19 23:50, Rich Shepard wrote:

In a database application I want to design a view for the table rows
returned from a select statement. Tkinter has a listbox widget and web
searches suggest that multicolumn listboxes are best based on ttk.Treeview
widgets, but my understanding of a treeview is to display a hierarchical set
rather than a simple list.

Each table has multiple columns and I want to create a view for each table
that will allow the user to select a row. The SQL select statement will be
formed from criteria provided in a separate dialog box.


Here's a small example.

#!python3.7
# -*- coding: utf-8 -*-
#
# Example demonstrating a multi-column table using ttk.Treeview and 
scrollbars.

#
import tkinter as tk
import tkinter.ttk as ttk

class TableExample(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)

self.title('Table example')

headings = ['English', 'Experanto']

self.grid_rowconfigure(0, weight=1)
self.grid_rowconfigure(1, weight=0)
self.grid_columnconfigure(0, weight=1)
self.grid_columnconfigure(1, weight=0)

vscrollbar = tk.Scrollbar(self, orient='vertical')
vscrollbar.grid(row=0, column=1, sticky='ns')

hscrollbar = tk.Scrollbar(self, orient='horizontal')
hscrollbar.grid(row=1, column=0, sticky='we')

self.column_ids = ['#%d' % h for h in range(1, len(headings) + 1)]

def fix_map(option):
# Fix for setting text colour for Tkinter 8.6.9
# From: https://core.tcl.tk/tk/info/509cafafae
#
# Returns the style map for 'option' with any styles 
starting with

# ('!disabled', '!selected', ...) filtered out.

# style.map() returns an empty list for missing options, so 
this

# should be future-safe.
return [elm for elm in style.map('Treeview', 
query_opt=option) if

  elm[:2] != ('!disabled', '!selected')]

style = ttk.Style()
style.map('Treeview', foreground=fix_map('foreground'),
  background=fix_map('background'))

self._table = ttk.Treeview(self, columns=self.column_ids,
  displaycolumns='#all', show=['headings'],
  yscrollcommand=vscrollbar.set, xscrollcommand=hscrollbar.set)
self._table.grid(row=0, column=0, sticky='nswe')

for id, heading in zip(self.column_ids, headings):
self._table.heading(id, text=heading)

vscrollbar.config(command=self._table.yview)
hscrollbar.config(command=self._table.xview)

# Now to fill the table.
words = [
('zero', 'nul'),
('one', 'unu'),
('two', 'du'),
('three', 'tri'),
('four', 'kvar'),
('five', 'kvin'),
]

for row in words:
# Add a new row.
row_id = self._table.insert('', 'end')

# Fill the new row.
for column_id, entry in zip(self.column_ids, row):
self._table.set(row_id, column_id, entry)

TableExample().mainloop()

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


Re: tkinter: widget to display set returned from database table

2019-06-19 Thread Terry Reedy

On 6/19/2019 6:50 PM, Rich Shepard wrote:

In a database application I want to design a view for the table rows
returned from a select statement. Tkinter has a listbox widget and web
searches suggest that multicolumn listboxes are best based on ttk.Treeview


Right.

widgets, but my understanding of a treeview is to display a hierarchical 
set rather than a simple list.


There is no sin is using less than all the features of a widget. 
Anyway, think of the tree having one node, which you can hide (not 
show), representing the table, and n leaves, representing the rows of 
the table, which you do show.


If you sort the selection by some categorical field, then you can make 
the display hierarchical by adding expandable rows for values of the 
sort field.  Think of database reports where summary lines can be 
expanded and contracted.


--
Terry Jan Reedy

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