[EMAIL PROTECTED] wrote:

Hello Jens, hello all,

thanks for your replay. I tried your solution first and get some errors.
Independing of the size in socket.recv and f.read I allways get an empty
datastring after 1770 bytes. But I found a similiar solution here
http://mail.python.org/pipermail/python-list/2002-July/111754.html. This code
uses struct instead of pickle and that works for me. I don't know if one of this
solutions has some advantages/disadvantages, but I will try it laiter again with
pickle.


That's weird. I cannot think of any reason why it would work with struct and not pickle. I specifically used pickle so I wouldn't have to worry about type truncation. etc. In Python a 'long' datatype is what os.stat returns for the size and is, specifically, potentially bigger than a 32-bit int. I figured that way I could very portably send the size without worrying about what architecture was on either end. In your case of course you could make stricter assumptions (same endianness, files less than 4GB) but I didn't want to post something for all the world to see that would have those types of limitations (of course, I didn't actually compile and test the code i sent per se--just check some of the statements--so clearly I wasn't being all /that/ careful).

But I have some more questions:
1) If the serverside runs in inifitiv loop, the 'break into running code'
funktion of pythonwin doesn't works, because it is a system call. Is there a way
to stop the programm using a shortcut ?


Sure, there are plenty of ways to stop it. Actually I would imagine that at some point you'd probably want to go one of two routes: either make it run as an NT Service or give it a GUI that maybe shows you a little indication when something is going, a short log of transfers, etc. In either case there would be an appropriate way of stopping the program. In the case of a service I would probably run the above in a separate thread and create an NT Event (win32event.CreateEvent) and then in the loop you can set up to wait on either of this event of a socket acceptance coming.

In the case of a gui I would do just about the same thing except that instead of the Service Control Manager dispatch being the place where the event gets signalled it you'd maybe add a button (and also in the close handler) to signal the event.

2) Maybe I want to install the serverside as a service like it is described in
Mark's book. Then step 1) can be solved by stopping the service. Is it save to
run it this way:



s = socket(AF_INET,SOCK_STREAM)
s.bind(('',PORT))
s.listen(1)
while True:
q,v = s.accept()
... rest of code, s and q are not closed





Nope, not safe really. Well, you could just exit the program when the service stop happens but that would be uncouth. The more elegant solution would be to do as I mentioned above and use an event. Unfortunately this complicates things a little bit. Either you can make the listening socket asynchronous (or do a settimeout )and poll it in a loop with a delay in between or you can do the more windowsy way and add a windows event. So let's say that we have a close_evt to signal that it is time to exit and a accept_evt to signal that we've received a socket connection. The code to do this looks like:

close_evt = win32event.CreateEvent(None, True, False, None)
accept_evt = win32event.CreateEvent(None, False, False, None)
win32file.WSAEventSelect(s, accept_evt, win32file.FD_ACCEPT)

(assuming the socket we are listening on is 's')

Then in our loop we do this:

while True :
retval = win32event.WaitForMultipleObjects((close_evt, accept_evt), False, win32event.INFINITE)
if retval == win32event.WAIT_OBJECT_0 :
# this means it is time to exit
win32file.CloseHandle(accept_evt)
s.close()
return
else if retval == win32event.WAIT_OBJECT_0 + 1 :
# this means we have received a socket connection
(client_sock, client_addr) = s.accept()
# ...


with the above construct we can make sure our "worker" thread isn't busy serving a client when we shut down. The way to complete this is to have the dispatch thread signal the event (win32event.SetEvent(close_evt)) and then wait for the worker thread to exit, then the program can exit.

or have I to use threads or what ever else to make sure that the socket is still
listening ? When I tried this code, I get sometimes 'conection refused' errors
after the serverside runs for longer time.

regards,
Jürgen

-------------------------------------------------
This mail sent through IMP: http://horde.org/imp/
_______________________________________________
Python-win32 mailing list
[EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/python-win32




--
Jens B. Jorgensen
[EMAIL PROTECTED]

"With a focused commitment to our clients and our people, we deliver value through customized technology solutions"

_______________________________________________
Python-win32 mailing list
[EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/python-win32

Reply via email to