Client/Server based on SocketServer and Windows

K

Kiki

Hello list,

I've written a small Client/server system.
Basically, i'm expecting something like : The client sends every once
and a while a small data chunk (not more than 50 bytes) the server
receive it and print it.

Here is the server request handler :

class ThreadedTCPRequestHandlerFoo(SocketServer.BaseRequestHandler):

def handle(self):
data = self.request.recv(1024)
cur_thread = threading.currentThread()
response = "%s: %s from Foo" % (cur_thread.getName(),
data)
print response

and this is the client :

def clientPrompt(ip, port, message):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((ip, port))
while(1):
k=raw_input('#>')
sock.send(k)
print "%s\n" % k
if k == 'quit': break
sock.close()

My problem comes from that I can't send data from client more than
once without having the following Winsock error : 10053 Software
caused connection abort.
I have to restart the client each time I want to send a new message.

Could anyboy explain me why ?

Regards
 
D

Dennis Lee Bieber

and this is the client :

def clientPrompt(ip, port, message):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((ip, port))
while(1):
k=raw_input('#>')
sock.send(k)
print "%s\n" % k
if k == 'quit': break
sock.close()

My problem comes from that I can't send data from client more than
once without having the following Winsock error : 10053 Software
caused connection abort.
I have to restart the client each time I want to send a new message.

Could anyboy explain me why ?
First, clean up your apparent mixed form indentation (in the read
window, both "while" and sock.close() are to the left of the rest when
using non-fixed font, and here, using a fixed font, the close is
indented under the "if". You have four different levels of indentation
for something that only uses two!

The above indentation, as it shows in this reply, wouldn't even run
in a Python interpreter -- the "while" should line up with the
sock.connect(), and all EXCEPT the sock.close() should be indented one
more level.

What is shown above is CLOSING the socket after the first read, NOT
after quitting the loop.

Oh, and unless you have an old (1.5.x) version of Python, an
infinite loop is written with just:

while True:

NO C-style () needed (never have been, unless using a complex boolean
expression)

-=-=-=-=-=-=-=-
def clientPrompt(ip, port, message = ""):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #indent 1
s.connect( (ip, port) )
while True:
k = raw_input( "\n%s #> " % message) #indent 2
sock.send(k)
print k
if k == "quit": break
sock.close() #out to 1
-=-=-=-=-=-=-=-
 
K

Kiki

Thank you Dennis

I'm using 2 differents editor, which may be the cause of such a mess
in the indentation.

I must admitt that I lazily rely on those (not so bad indeed) editors.

"If indentation whas bad they would have tell me"

Too bad am i

Won't post misindeted code anymore.
 
D

Dave Angel

Kiki said:
Thank you Dennis

I'm using 2 differents editor, which may be the cause of such a mess
in the indentation.

I must admitt that I lazily rely on those (not so bad indeed) editors.

"If indentation whas bad they would have tell me"

Too bad am i

Won't post misindeted code anymore.
No problem using multiple editors. But you need to configure each one
to *never* put tabs in the file (it should use spaces exclusively). If
you can't do that in one of the editors, then use the other editor
exclusively.

Other useful but not mandatory features -
- when tab is pressed, it should indent using spaces, to a multiple
of 4 columns.
- by default, indent a new line the same as the previous, or even
based on the previous plus some syntax rules.
- make tabs and/or spaces visible, so you can tell if the file has a
mixture.

DaveA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top