[Tutor] Tutor Digest, Vol 131, Issue 14
pramod gowda
pramod.sp78 at gmail.com
Sat Jan 3 16:58:50 CET 2015
Hello ,
I have chaged the code as per your input, except port number.
If i change the port number it give me error as
Traceback (most recent call last):
File "C:\Users\Pramod\Desktop\client.py", line 7, in <module>
client_socket.connect((server_address,server_port))
ConnectionRefusedError: [WinError 10061] No connection could be made
because the target machine actively refused it
Server code:
import socket
server_socket=socket.socket()
server_name='192.168.2.2'
server_port= 80
server_socket.bind((server_name,server_port))
server_socket.listen(5)
while True:
print("hello")
c,address=server_socket.accept()
print("acception the connection")
print("we got connection from:",address)
c.send("hello,hw ru")
c.close()
Client Code:
import socket
client_socket=socket.socket()
server_address='192.168.2.2'
server_port= 80
print("port number entered")
client_socket.connect((server_address,server_port))
print("it is connected")
data=client_socket.recv(1024)
print("received:",data)
client_socket.close()
print('finished')
Output :
port number entered
it is connected
received: *b''*
finished
In the above output expected data is 'hello,hw ru' from the server and
print("acception the connection")
print("we got connection from:",address)
the above print statements are not executed.
Thanks and Regards,
Pramod SP
Regards,
Pramod SP
On Sat, Jan 3, 2015 at 8:39 PM, <tutor-request at python.org> wrote:
> Send Tutor mailing list submissions to
> tutor at python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
> https://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body 'help' to
> tutor-request at python.org
>
> You can reach the person managing the list at
> tutor-owner at python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Tutor digest..."
>
>
> Today's Topics:
>
> 1. Re: Improving My Simple Game Code for Speed, Memory and
> Learning (Dave Angel)
> 2. Socket programming (pramod gowda)
> 3. Re: Socket programming (Alan Gauld)
> 4. Re: Improving My Simple Game Code for Speed, Memory and
> Learning (Steven D'Aprano)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Sat, 03 Jan 2015 06:58:34 -0500
> From: Dave Angel <davea at davea.name>
> To: tutor at python.org
> Subject: Re: [Tutor] Improving My Simple Game Code for Speed, Memory
> and Learning
> Message-ID: <54A7D96A.6040606 at davea.name>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> On 01/02/2015 10:21 PM, Dave Angel wrote:
> > On 01/02/2015 09:00 PM, WolfRage wrote:
> >> Python3.4+ Linux Mint 17.1 but the code will be cross platform (Mobile,
> >> Windows, Linux, OSX).
> >>
> >> First an explanation of how the game works: The game is a simple
> >> matching game but with a twist. Instead of matching a straight 3 in a
> >> row, we have some rules that only certain combinations will result in an
> >> elimination. In general 2 of the same value in a row with with 1 of 2
> >> other possible values will eliminate all three nodes. Eliminations can
> >> occur either horizontally or vertically but not diagonally. Each tile
> >> has a value now, that value is used when evaluating the rules. Currently
> >> there are only 5 allowed values of 0-4 although more could be added
> >> later, so any solution needs to be expandable.
> >> These are the basic rules that allow for an elimination to occur(values
> >> are put in quotes to distinguish values):
> >> 2 "5"s followed or proceeded by a "19" will eliminate all 3 nodes.
> >> 2 "5"s followed or proceeded by an "11" will eliminate all 3 nodes.
> >> 2 "6"s followed or proceeded by a "5" will eliminate all 3 nodes.
> >> 2 "6"s followed or proceeded by a "19" will eliminate all 3 nodes.
> >> 2 "11"s followed or proceeded by a "6" will eliminate all 3 nodes.
> >> 2 "11"s followed or proceeded by a "20" will eliminate all 3 nodes.
> >> 2 "19"s followed or proceeded by a "20" will eliminate all 3 nodes.
> >> 2 "19"s followed or proceeded by an "11" will eliminate all 3 nodes.
> >> 2 "20"s followed or proceeded by a "6" will eliminate all 3 nodes.
> >> 2 "20"s followed or proceeded by a "5" will eliminate all 3 nodes.
> >>
> >> The main focus of the code is the find_eliminations method. I think its
> >> implementation is smart since it uses a math trick, but then again I
> >> wrote it.
> >> My math trick works by first finding 2 matching nodes next to each other
> >> out of every 3 nodes, then adding the values of all 3 nodes together and
> >> checking for specific totals. None of the possible totals overlap.
> >>
> >> Here is the code:
> >>
> >> import random
> >>
> >>
> >> class GameTile():
> >> def __init__(self, value, col, row, **kwargs):
> >> # id is grid (X,Y) which is equal to grid (col,row)
> >> self.id = str(col) + ',' + str(row)
> >> self.col = col
> >> self.row = row
> >> self.value = value
> >> self.eliminated = False
> >>
> >> def __str__(self):
> >> #return '%d, %d' % (self.col, self.row)
> >> if len(str(self.value)) == 1:
> >> return ' ' + str(self.value)
> >> return str(self.value)
> >>
> >>
> >> class GameGrid():
> >> def __init__(self, cols=8, rows=7, **kwargs):
> >> self.cols = cols
> >> self.rows = rows
> >> self.make_grid()
> >>
> >> def make_grid(self):
> >> # grid is 2d array as x, y ie [x][y].
> >> self.grid = []
> >> for row_num in range(self.rows):
> >> self.grid.append([GameTile(value=random.choice([5, 6, 11,
> >> 19, 20]), col=col_num,
> >> row=row_num) for col_num in range(self.cols)])
> >>
> >> def print_by_col(self):
> >> # As in going down the column assuming we started at the top.
> >> for col_num in range(self.cols):
> >> for row_list in self.grid:
> >> print(row_list[col_num])
> >>
> >> def print_by_row(self):
> >> # As in going right across the row assuming we started at the
> >> left.
> >> for row in self.grid:
> >> for node in row:
> >> print(node)
> >>
> >> def check_bounds(self, x, y):
> >> return (0 <= x < self.rows) and (0 <= y < self.cols)
> >>
> >> def lookup_node(self, x, y):
> >> if not self.check_bounds(x, y):
> >> return False
> >> return self.grid[x][y]
> >>
> >> def draw(self):
> >> for col in self.grid:
> >> print(end='| ')
> >> for node in col:
> >> print(node, end=' | ')
> >> print()
> >>
> >> def find_eliminations(self):
> >> # I define 3 variables for holding the 3 nodes/tiles that I am
> >> # currently checking to see if an elimination possibility
> >> exists.
> >> # It uses a math trick to check for elimination by adding the
> >> values
> >> # and checking for specific totals. None of the possible totals
> >> overlap.
> >> #First Down the columns.
> >> for col_num in range(self.cols):
> >> first = None
> >> second = None
> >> third = None
> >> for row_list in self.grid:
> >> if row_list[col_num].row == 0:
> >> first = row_list[col_num]
> >> elif row_list[col_num].row == 1:
> >> second = row_list[col_num]
> >> elif row_list[col_num].row == 2:
> >> third = row_list[col_num]
> >> else:
> >> first = second
> >> second = third
> >> third = row_list[col_num]
> >
> > This code is way too complex for what it accomplishes. See below.
> >
> >> if third is not None:
> >> self.check_total_and_eliminate(first, second,
> third)
> >> # Now across the rows.
> >> for row in self.grid:
> >> first = None
> >> second = None
> >> third = None
> >> for node in row:
> >> if node.col == 0:
> >> first = node
> >> elif node.col == 1:
> >> second = node
> >> elif node.col == 2:
> >> third = node
> >> else:
> >> first = second
> >> second = third
> >> third = node
> >
> > If the self.rows is 3, this is equivalent to just:
> > first, second, third = row
> > If the self.rows is larger, it's equivalent to:
> > first, second, third = row[p: p+3] for some value of p
> >
> >> if third is not None:
> >> self.check_total_and_eliminate(first, second,
> third)
> >
> > But even better, let the method check_total_and_eliminate take a slice
> > as its argument.
> > self.check-total_and_eliminate(row[p: p+3])
> >
> >> # Set all eliminated nodes to a value of 0.
> >> for col in self.grid:
> >> for node in col:
> >> if node.eliminated is True:
> >> node.eliminated = False
> >> node.value = 0
> >>
> >> def check_total_and_eliminate(self, first, second, third):
> >> total = None
> >> if first.value == second.value:
> >> total = first.value + second.value + third.value
> >> elif second.value == third.value:
> >> total = first.value + second.value + third.value
> >> if total == 17 or total == 21 or total == 28 or total == 29 or
> \
> >> total == 31 or total == 42 or total == 45 or total == 46 \
> >> or total == 49 or total == 58:
> >> first.eliminated = True
> >> second.eliminated = True
> >> third.eliminated = True
> >>
> >
> > This doesn't come close to implementing the test you describe above. For
> > example, a total of 29 could be your first case, 5,5,19. But it could
> > also be 4,4,21. Or 6,6,17. Etc.
> >
> > I suggest instead that you compare the values against a table of
> > interesting values. If you make your table a list of 3-tuples, it can
> > be searched simply by:
> > if tuple(nodes) in table:
> > do-something
>
> My error. Since nodes are GameTile objects, not values, converting to a
> tuple is a little trickier:
>
> values = tuple( [node.value for node in nodes] )
> if values in table:
> do-something
>
> >
> > So you don't need the separate variables first, second, and third at all.
> >
> >>
> >>
> >> grid = GameGrid(4, 8)
> >> grid.draw()
> >> grid.find_eliminations()
> >> print('After Eliminations')
> >> grid.draw()
> >>
> >>
> >> # END CODE
> >>
> >> After this part has been improved, I then need to search the columns
> >> backwards to be able to drop any floating values. Any none zero values
> >> should not have a zero value below it. That is what I mean by drop
> >> floating values. I think this will be simple by just using range method
> >> to count backwards. I will be working on coding that in the meantime.
> >>
> >
> > for what you've described so far, it might be convenient to store the
> > board and its transposed equivalent. Then search the one for columns,
> > and the other for rows. That's a later optimization, but it might save
> > time getting the more complicated parts figured out.
> >
>
> For example, print_by_col would look just like print_by_row, except that
> it would start with self.transposed_grid
>
> The only real assumption needed for this simplification is that once the
> grid is set up, you never create any more GameTile objects, just
> manipulate the existing rectangle of objects.
>
> To transpose a grid, you want to use the zip() function.
> self.transposed_grid = zip(*self.grid)
>
>
> --
> DaveA
>
>
> ------------------------------
>
> Message: 2
> Date: Sat, 3 Jan 2015 17:18:02 +0530
> From: pramod gowda <pramod.sp78 at gmail.com>
> To: tutor at python.org
> Subject: [Tutor] Socket programming
> Message-ID:
> <CAAqnBO9BgjtzNWrn5DwQLnrV3doLpq4zQVZ9r=
> kGJG51V3ywjw at mail.gmail.com>
> Content-Type: text/plain; charset=UTF-8
>
> Hi,
>
> i am learning socket programming,
>
> client code:
>
> import socket
>
> client_socket=socket.socket()
> server_address='192.168.2.2'
> server_port= 80
> print("hello")
> client_socket.connect((server_address,server_port))
> print("hello")
> data=client_socket.recv(1024)
> print(data)
> client_socket.close()
>
> server code:
> import socket
>
> server_socket=socket.socket()
> server_name='192.168.2.2'
> server_port= 80
> server_socket.bind((server_name,server_port))
> server_socket.listen(1) 3.4.2
>
> while True:
> print("hello")
> c,address=server_socket.accept()
> print("we got connection from:",address)
> c.send("hello,hw ru")
> c.close()
>
>
>
>
> I am not getting the output, i am using windows 7 OS,pyhton ver:..
> please check and give me the solution.
> Regards,
>
> Pramod SP
>
>
> ------------------------------
>
> Message: 3
> Date: Sat, 03 Jan 2015 13:20:01 +0000
> From: Alan Gauld <alan.gauld at btinternet.com>
> To: tutor at python.org
> Subject: Re: [Tutor] Socket programming
> Message-ID: <m88q9u$i03$1 at ger.gmane.org>
> Content-Type: text/plain; charset=windows-1252; format=flowed
>
> On 03/01/15 11:48, pramod gowda wrote:
>
> > client code:
> >
> > import socket
> >
> > client_socket=socket.socket()
> > server_address='192.168.2.2'
> > server_port= 80
>
> see below regarding port numbers
>
> > print("hello")
> > client_socket.connect((server_address,server_port))
> > print("hello")
> > data=client_socket.recv(1024)
> > print(data)
> > client_socket.close()
>
>
> I assume it is this code that is not doing what you expect?
> Try using more descriptive print statements.
> Also, when debugging, never use a print that could be blank,
> always have a fixed string so you can see it. So instead of
>
> print(data)
>
> use
>
> print('Received: ', data)
>
> That way you can see that data was empty rather than that
> the print didn't execute.
>
> I'd also put a print(finished') or similar after closing
> the socket.
>
> > server code:
> > import socket
> >
> > server_socket=socket.socket()
> > server_name='192.168.2.2'
> > server_port= 80
>
> port 80 is usually reserved for http traffic, better
> to choose a port number greater than 1024 for user
> programs.
>
> > server_socket.bind((server_name,server_port))
> > server_socket.listen(1)
>
> I'd up the queue size to at least 3 or 5.
> You are not leaving much room for errors.
>
> > while True:
> > print("hello")
> > c,address=server_socket.accept()
> > print("we got connection from:",address)
>
> Is this print appearing OK?
>
> > c.send("hello,hw ru")
>
> I'd add a print here too, to verify that the send worked.
>
> > c.close()
>
> > I am not getting the output, i am using windows 7 OS,pyhton ver:..
> > please check and give me the solution.
>
> It's not clear what you mean by "the output".
> Are you getting anything printed? At the client and the server?
>
> It might help to show us a cut n paste from both client
> and server.
>
> --
> 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
>
>
>
>
> ------------------------------
>
> Message: 4
> Date: Sun, 4 Jan 2015 02:09:19 +1100
> From: Steven D'Aprano <steve at pearwood.info>
> To: tutor at python.org
> Subject: Re: [Tutor] Improving My Simple Game Code for Speed, Memory
> and Learning
> Message-ID: <20150103150916.GF27426 at ando.pearwood.info>
> Content-Type: text/plain; charset=us-ascii
>
> On Fri, Jan 02, 2015 at 09:00:22PM -0500, WolfRage wrote:
> > Python3.4+ Linux Mint 17.1 but the code will be cross platform (Mobile,
> > Windows, Linux, OSX).
> >
> > First an explanation of how the game works: The game is a simple
> > matching game but with a twist. Instead of matching a straight 3 in a
> > row, we have some rules that only certain combinations will result in an
> > elimination. In general 2 of the same value in a row with with 1 of 2
> > other possible values will eliminate all three nodes.
>
> Do you mean "1 of 2" or "1 or 2"?
>
>
> > Eliminations can
> > occur either horizontally or vertically but not diagonally. Each tile
> > has a value now, that value is used when evaluating the rules. Currently
> > there are only 5 allowed values of 0-4 although more could be added
> > later, so any solution needs to be expandable.
>
> Okay, so the values are 0, 1, 2, 3, and 4. This doesn't match your code
> belong. Is the code wrong or your description?
>
>
> > These are the basic rules that allow for an elimination to occur(values
> > are put in quotes to distinguish values):
> > 2 "5"s followed or proceeded by a "19" will eliminate all 3 nodes.
> [...]
>
> Since neither 5 nor 19 is a possible value, this can never apply.
>
> All the other rules you give include values greater than 4, and likewise
> can never apply. That means that no eliminations are ever possible. I
> think your description needs some work :-)
>
>
> > class GameTile():
> [...]
> > def __str__(self):
> > #return '%d, %d' % (self.col, self.row)
> > if len(str(self.value)) == 1:
> > return ' ' + str(self.value)
> > return str(self.value)
>
> Eliminate comments which no longer apply. Don't use them for keeping
> old, obsolete or wrong code.
>
> This method can be better written like this:
>
> def __str__(self):
> return "%2d" % self.value
>
>
> > class GameGrid():
> > def __init__(self, cols=8, rows=7, **kwargs):
> > self.cols = cols
> > self.rows = rows
> > self.make_grid()
> >
> > def make_grid(self):
> > # grid is 2d array as x, y ie [x][y].
> > self.grid = []
> > for row_num in range(self.rows):
> > self.grid.append([GameTile(value=random.choice([5, 6, 11,
> > 19, 20]), col=col_num,
> > row=row_num) for col_num in range(self.cols)])
>
> That's rather gnarly code. If you're going to do that, it's probably
> nice to use a temporary variable to clarify your intention:
>
> for row_num in range(self.rows):
> row = [GameTile(
> value=random.choice([5, 6, 11,19, 20]), col=col_num,
> row=row_num) for col_num in range(self.cols)
> ]
> self.grid.append(row)
>
>
> Which is still pretty gnarly. You only have three arguments to GameTile,
> why do you need to use keyword arguments? This is neater and less
> noisy, although it does require you get the order right:
>
> # untested
> for row_num in range(self.rows):
> row = [GameTile(row_num, col_num, random.choice([5, 6, 11,19,
> 20])
> for col_num in range(self.cols)
> ]
> self.grid.append(row)
>
> Even better:
>
> for row_num in range(self.rows):
> self.make_row(row_num)
>
> def make_row(self, row_num):
> values = self.allowed_values # [5, 6, 11,19, 20]
> row = [GameTile(row_num, col_num, random.choice(values)
> for col_num in range(self.cols)]
> self.grid.append(row)
>
>
> Notice that the values are stored in an attribute, for ease of
> maintanence in the future when you extend them.
>
>
> > def find_eliminations(self):
> > # I define 3 variables for holding the 3 nodes/tiles that I am
> > # currently checking to see if an elimination possibility exists.
> > # It uses a math trick to check for elimination by adding the
> values
> > # and checking for specific totals. None of the possible totals
> overlap.
>
> Sorry, I'm too tired to make sense of this function right now :-(
>
>
> > def check_total_and_eliminate(self, first, second, third):
> > total = None
> > if first.value == second.value:
> > total = first.value + second.value + third.value
> > elif second.value == third.value:
> > total = first.value + second.value + third.value
> > if total == 17 or total == 21 or total == 28 or total == 29 or \
> > total == 31 or total == 42 or total == 45 or total == 46 \
> > or total == 49 or total == 58:
> > first.eliminated = True
> > second.eliminated = True
> > third.eliminated = True
>
> I think this method does too much. I would prefer to see it split into
> two methods, one to check the total, and the other to eliminate the
> cells if needed:
>
> def eliminate(self, first, second, third):
> first.eliminated = True
> second.eliminated = True
> third.eliminated = True
>
> def check_total(self, first, second, third):
> if first.value == second.value or second.value == third.value:
> total = first.value + second.value + third.value
> return total in (17, 21, 28, 29, 31, 42, 45, 46, 49, 58)
>
>
> which you then use:
>
> if self.check_total(first, second, third):
> self.eliminate(first, second, third)
>
>
>
>
> --
> Steven
>
>
> ------------------------------
>
> Subject: Digest Footer
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> https://mail.python.org/mailman/listinfo/tutor
>
>
> ------------------------------
>
> End of Tutor Digest, Vol 131, Issue 14
> **************************************
>
More information about the Tutor
mailing list