Learning python networking

2014-01-08 Thread Paul Pittlerson
I'm trying to learn about socket, how to create and handle connections in 
python.

This is the best I could come up with on my own, please take a look and give me 
critique:
Server script:
http://pastebin.com/KtapYfM0

Client script:
http://pastebin.com/t4dYygmX

How to run it:
I open 3 terminals, in one I start the server script, and enter into it 
something like 'accept 2'

Then in the two other terminals I start client scripts which will "connect to 
the server" so to speak.

Now I can communicate between them by sending messages, and exit the whole 
operation by typing 'exit' into the server.

Is the code overly complicated? More precisely: is there a more elegant and 
simple way to achieve the same thing? Some people have mentioned things like 
twisted and asyncore, but I don't know anything about them. If it turns out 
this kind of concept is very straight forward to set up in either of those I 
would be interested in sample code.

I'm specifically looking into this kind of communication because I want to make 
a small multiplayer game.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Constructive Criticism

2014-01-08 Thread Paul Pittlerson
I think the only winning move is not to play.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Learning python networking

2014-01-15 Thread Paul Pittlerson
I'm sorry if this is a bit late of a response, but here goes.

Big thanks to Chris Angelico for his comprehensive reply, and yes, I do have 
some questions!


> On Thursday, January 9, 2014 1:29:03 AM UTC+2, Chris Angelico wrote:
> Those sorts of frameworks would be helpful if you need to scale to
> infinity, but threads work fine when it's small.
That's what I thought, but I was just asking if it would be like trivially easy 
to set up this stuff in some of those frameworks. I'm sticking to threads for 
now because the learning curve of twisted seems too steep to be worth it at the 
moment.

> Absolutely! The thing to look at is MUDs and chat servers. Ultimately,
> a multiplayer game is really just a chat room with a really fancy
> front end.
If you know of any open source projects or just instructional code of this 
nature in general I'll be interested to take a look. For example, you mentioned 
you had some similar projects of your own..?


> The server shouldn't require interaction at all. It should accept any
> number of clients (rather than getting the exact number that you
> enter), and drop them off the list when they're not there. That's a
> bit of extra effort but it's hugely beneficial.
I get what you are saying, but I should mention that I'm just making a 2 player 
strategy game at this point, which makes sense of the limited number of 
connections.

> One extremely critical point about your protocol. TCP is a stream -
> you don't have message boundaries. You can't depend on one send()
> becoming one recv() at the other end. It might happen to work when you
> do one thing at a time on localhost, but it won't be reliable on the 
> internet or when there's more traffic. So you'll need to delimit
> messages; I recommend you use one of two classic ways: either prefix
> it with a length (so you know how many more bytes to receive), or
> terminate it with a newline (which depends on there not being a
> newline in the text).
I don't understand. Can you show some examples of how to do this?

> Another rather important point, in two halves. You're writing this for
> Python 2, and you're writing with no Unicode handling. I strongly
> recommend that you switch to Python 3 and support full Unicode.
Good point, however the framework I'm using for graphics does not currently 
support python3. I could make the server scripts be in python3, but I don't  
think the potential confusion is worth it until the whole thing can be in the 
same version.


> Note, by the way, that it's helpful to distinguish "data" and "text",
> even in pseudo-code. It's impossible to send text across a socket -
> you have to send bytes of data. If you keep this distinction clearly
> in your head, you'll have no problem knowing when to encode and when
> to decode. For what you're doing here, for instance, I would packetize
> the bytes and then decode into text, and on sending, I'd encode text
> (UTF-8 would be hands-down best here) and then packetize. There are
> other options but that's how I'd do it.
I'm not sure what you are talking about here. Would you care to elaborate on 
this please (it interests and confuses) ?


I'm posting this on google groups, so I hope the formatting turns out ok :P 
thanks.
-- 
https://mail.python.org/mailman/listinfo/python-list


How to check client shutdown?

2013-08-26 Thread Paul Pittlerson
I'm currently learning about the socket module. My question is how can I detect 
if a connection is closed from the other side, for example a KeyboardInterrupt 
as I frequently use. My code below:

##
#server script:

class client(threading.Thread):
def __init__(self, connection):
super(client, self).__init__()

self.recv = connection.recv

print dir(connection)

def start(self):
self.run()

def run(self):
while True:
data = self.recv(2000)

if data:
msg = self.decode(data)
print msg
#break;

def decode(self, data):
return pickle.loads(data)


class bullet_server:
def __init__(self):
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

def start(self):
server_address = ("localhost", 9006)
print '/--server running--/'
print server_address 
self.server.bind(server_address)
self.server.listen(5)

while True:
connection, client_address = self.server.accept()

c = client(connection)
c.start()


if __name__ == '__main__':
server = bullet_server()
server.start()


##
#client script:

# set IP and port
host = '127.0.0.1'
port = 9006

# connect to server
connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connection.connect((host, port))

def loop():
print 'Enter message:'
msg = raw_input()
encoded = pickle.dumps(msg)
connection.send(encoded)
loop()

loop()


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


Re: How to check client shutdown?

2013-08-27 Thread Paul Pittlerson
> MAJOR security issue here. You are taking data from a networked source
> 
> and running it through a trusting system (pickle). This is NOT
> 
> recommended.
> 

Security issue!? Do you mean someone could enter devious python h4xx into the 
chat or something? I had no idea using pickle was so dangerous, but I don't 
know any other method of transmitting data in python :(



> Also: You're using a stream socket, and depending - I think - on
> 
> getting an entire message in a single read() call. This is definitely
> 
> not guaranteed, though when you're working with localhost it's likely
> 
> to be true. To make this reliable, you'll need to do something like
> 
> prefix the message with its length (eg a Hollerith string), or use
> 
> some system like JSON that lets you detect the end of the message.
> 

I'm guessing the fix is to have a separate thread which only job is to send 
info about the size of the next data transmission. What is the actual downside 
of having the server set to anticipate a message length which is known to be 
more than will be sent (or be allowed to be sent?), for example 
connection.recv(1). Does not the receiver know the size after the fact? Is 
it impacting performance somehow (I haven't noticed anything in my tests)

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


Multiprocessing / threading confusion

2013-09-05 Thread Paul Pittlerson
I'm trying to understand data handling using multiprocessing and threading, 
haven't gotten very far without running into problems. This is my code:

#!/usr/bin/python

from multiprocessing import Process
from multiprocessing import Queue
from multiprocessing import current_process

from threading import Thread

import time

def gogo(qu):
w = Worker(qu)
w.start()

class Worker(Thread):
def __init__(self, Que):
super (Worker, self).__init__()

self._pid = current_process().pid  

self.que = Que
self.que.put('started worker %s' % self._pid)

def run(self):
self.que.put('%s ticked' % self._pid)

def __del__(self):
self.que.put('%s has exited' % self._pid)

class Debugger(Thread):
def __init__(self, q):
super(Debugger, self).__init__()

self.q = q

def run(self):
while True:
time.sleep(1)

if not self.q.empty():
print self.q.get()

else: break;

if __name__ == '__main__':

debug_q = Queue()

debug = Debugger(debug_q)
debug.start()

for i in range(5):
d = Process(target=gogo, args=(debug_q,))
d.start()

What I expect to happen is the Debugger object will receive one string at a 
time, and read it from the queue. But that's not what I see the the output, the 
"started worker" stuff seems to print for every process, but "ticked" and 
"exited" will show up in unpredictable ways, I'm guessing they overwrite each 
other and therefore will not always appear in the output.

So I'm looking for help in trying to make sense of this kind of stuff, I 
thought this was the basic functionality that Queue() would take care of unless 
there is some other problem in my code.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Multiprocessing / threading confusion

2013-09-05 Thread Paul Pittlerson
On Friday, September 6, 2013 1:28:39 AM UTC+3, mar...@python.net wrote:

> Also you can't (and shouldn't) depend on the time that __del__ gets
> called.  It can get called at any time, in any order and sometimes not
> at all.*

Wow I did not know that! I was counting on that it reliably gets called when 
the object is destroyed.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Multiprocessing / threading confusion

2013-09-05 Thread Paul Pittlerson
On Friday, September 6, 2013 1:46:40 AM UTC+3, Chris Angelico wrote:

> The first thing I notice is that your Debugger will quit as soon as
> its one-secondly poll results in no data. This may or may not be a
> problem for your code, but I'd classify it as code smell at best. Is
> your goal here to make sure Debugger doesn't stop your process from
> exiting? If so, a simpler and safer solution is to make it a daemon
> thread.

I didn't think it would be a problem, because unless the system is very
slow, the functions will finish in a fraction of a second, on my machine
it does not matter whether I have it set as 0.1 second or several seconds,
the output is still the same. It's not eloquent, but the point was just to
exit the test when no more prints are to be made.

But how can I fix the actual bug I was asking about though? I want to 
print ticked and exited for all the processes, just to acknowledge to
myself that the code is working.. so I can proceed to experiment with
more complexity! :D
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Multiprocessing / threading confusion

2013-09-06 Thread Paul Pittlerson
Ok here is the fixed and shortened version of my script:

#!/usr/bin/python

from multiprocessing import Process, Queue, current_process
from threading import Thread
from time import sleep

class Worker():
def __init__(self, Que):
self._pid = current_process().pid
self.que = Que
self.que.put('started worker %s' % self._pid)

for n in range(5):
self.que.put('%s tick %d' % (self._pid, n))
# do some work
sleep(0.01)

self.que.put('%s has exited' % self._pid) 

class Debugger(Thread):
def __init__(self, q):
super(Debugger, self).__init__()
self.q = q

def run(self):
while True:

sleep(0.1)

if not self.q.empty():
print self.q.get()

else:
break
#

if __name__ == '__main__':

debug_q = Queue()
debug = Debugger(debug_q)
debug.start()

for i in range(5):

d = Process(target=Worker, args=(debug_q,))
d.start()

This works great on linux, but does not run on windows (7). The behavior was: I 
opened it with double clicking and so a window appeared and disappeared (no 
text) then I opened it with IDLE and ran it there, where it worked a couple 
times. Then reopened it with IDLE and this time it did not work at all. After 
that the script did not run either through IDLE or opening directly.

What may be the reason it works on linux, but seems buggy on windows?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: better and user friendly IDE recommended?

2013-09-12 Thread Paul Pittlerson
On Wednesday, September 11, 2013 5:14:04 PM UTC+3, mnishpsyched wrote:
> Hey i am a programmer but new to python. Can anyone guide me in knowing which 
> is a better IDE used to develop web related apps that connect to DB using 
> python?

If you are a programmer in the sense that you are a proficient in something like
C or Java, then whatever development environment you used for that type of 
coding
will surely work just as well for writing python scripts.

But to answer your question: If you want something modern and fresh, maybe give 
Sublime Text a try. Personally, my favorite is Geany, because it has all the 
functionality and customization I want from an editor, but is not very bloated.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Sexism in the Ruby community: how does the Python community manage it?

2013-10-17 Thread Paul Pittlerson
What we need to do is A) Prove that we are not sexist and racist by excluding 
and intolerating people who do not agree with. B) Head on over to the Ruby 
mailing list and make a thread called "Hey guys we are the python people, and 
can you learn to behave, ok plz?" wherein we detail to them what we think they 
are doing wrong and how we, in our superior moral judgment, think they ought to 
improve themselves, that'll teach them not to be sexist and racist alright.

I think that is what OP is suggesting we do, if it's not then I really don't 
know what it is.

I've sometimes been curious why programming in general, and python included is 
a male dominated field, well I'm glad that OP has figured out it's just because 
we scare all the women away with our overt sexism and racism naming 
conventions. 

Great thread, just great.
-- 
https://mail.python.org/mailman/listinfo/python-list


pycrypto: what am I doing wrong?

2013-10-23 Thread Paul Pittlerson
I seem to have misunderstood something about the way Crypto.Cipher is supposed 
to work, because I'm getting unexpected results, here is my code..

import hashlib
from Crypto.Cipher import AES
from Crypto import Random

h = hashlib.new('sha256')
h.update('my key')
key = h.digest()

iv = Random.new().read(AES.block_size)
cipher = AES.new(key, AES.MODE_CFB, iv)
txt = 'hello world'

# This is the part where I'm confused, because it seems like encrypt will 
output a different result every time, so how can I decrypt it?

msg = cipher.encrypt(txt)

>>> '|s\x08\xf2\x12\xde\x8cD\xe7u*'

msg = cipher.encrypt(txt)

>>> '\xa1\xed7\xb8h>> '\x0b\xd9\x9f0\xd1\xb9E\x81;\x8a\xd4\xff\xdb\xd4\x83\x84\xbd$=\xf3\xaf@a8t\xd8Bz<\xce\xe26hello
>>>  world'

# But it does not work subsequently:

msg = iv + cipher.encrypt(txt)
cipher.decrypt(iv+msg)

>>> '\xfb\xa1\xa8\x9e"L<\x10Rg\xb5f^\x8a\x17\xfd\xbd$=\xf3\xaf@a8t\xd8Bz<\xce\xe26\xde\xc6cD\xdal\'\xf3@(\xa6'

What am I doing wrong?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: WHAT DOES ISLAM SAY ABOUT TERRORISM ?????????????????

2013-11-09 Thread Paul Pittlerson
If you want to convert pythoneers to Islam, you are gonna have to show them how 
importing Allah will make their scripts run faster, or something like that. 
Otherwise, I'm pretty sure you are out of luck.
-- 
https://mail.python.org/mailman/listinfo/python-list


unit selection problem

2013-01-14 Thread Paul Pittlerson
Unit selection doesn't work properly. Pygames event.pos returns a tuple of the 
coords within the current window, so it's not possible to select units outside 
of the top left corner.

from pygamehelper import *
from pygame import *
from pygame.locals import *
from vec2d import *
from math import e, pi, cos, sin, sqrt
from random import uniform
from mapeditor import *
from entity import *
from spritesheet import *

# version alpha 0.1
# map drawing seems functional, albeit crude

class Starter(PygameHelper):
def __init__(self):
self.w, self.h = 1600, 900
PygameHelper.__init__(self, size=(self.w, self.h), fill=((255,255,255)))
self.map_layout = map_constructor("map.png", 2400)
self.object_counter = len(self.map_layout.all_map_objects)
map_textures = get_sprites( (48, 48) ,"spritesheet.png" , (0, 0) )
self.collision_map = []

# defines all map objects in readable format
self.map_sprite_list = {
'floor_red' : map_textures.sprites[0],
'floor_dark' : map_textures.sprites[2],
'wall' : map_textures.sprites[1],
'proto_unit' : map_textures.sprites[3],
}
# map scrolling modifiers
self.mod_w = 0
self.mod_h = 0

# introduce agents
self.agent_list = []
self.selected = []

# TODO: make surface size relative to map size
self.mapSurf = pygame.Surface( (3600, 3600) )

# draw the floor once

for e in self.map_layout.fixed_list:
if e.type == 1:
self.mapSurf.blit(self.map_sprite_list['floor_red'], e.rect)

self.mapSurfRect = self.mapSurf.get_rect()

# create a collision map based on wall positions
for e in self.map_layout.all_map_objects:   
if e.type == 2:
wall_object = wall()
wall_object.pos = vec2d((e.x_position + 24), (e.y_position + 
30))
wall_object.target = wall_object.pos  
self.collision_map.append(wall_object)

for e in self.map_layout.all_map_objects: # check map for agents
if e.type == 3:
a = agent()
a.pos = vec2d(e.x_position, e.y_position)
print a.pos
a.target = a.pos
self.agent_list.append(a)

def update(self):
# map scrolling
self.mapSurfRect.center = (((self.w / 2) + self.mod_w), (((self.h / 2)) 
+ self.mod_h))

# update agent position
for a in self.agent_list:
dir = a.target - a.pos
if dir.length >= 3:
dir.length = 3
a.pos = a.pos + dir

# update the walls, so they are drawn above the floor
for e in self.map_layout.fixed_list:   
if e.type == 2:
self.mapSurf.blit(self.map_sprite_list['wall'], e.rect)

# add agents to the mapSurf
for a in self.agent_list:
self.mapSurf.blit(self.map_sprite_list['proto_unit'], a.pos)


def keyUp(self, key):
pass

def keyDown(self, key):

# move the mapSurf object
# TODO: implement continuous camera movement while key is pressed
if key == K_RIGHT:
self.mod_w -= 100
if key == K_LEFT:
self.mod_w += 100
if key == K_UP:
self.mod_h += 100
if key == K_DOWN:
self.mod_h -= 100
  

def mouseUp(self, button, pos):

# select agent
if button==1:
print pos
for a in self.agent_list:
if a.pos.get_distance(vec2d(pos)) < 24:
self.selected.append(a)
print 'selected'

# right click to move selected agents
if button==3:
for a in self.selected:
a.target = vec2d(pos)


def mouseMotion(self, buttons, pos, rel):
pass

def draw(self):
#self.screen.fill((255,255,255))
self.screen.fill((0,0,0))

self.screen.blit(self.mapSurf, self.mapSurfRect)

#for a in self.selected:
#pygame.draw.circle(self.mapSurf, (50,200,50), a.pos.inttup(), 18, 
1) # represent selected units

s = Starter()
s.mainLoop(100)
-- 
http://mail.python.org/mailman/listinfo/python-list