[Tutor] Socket connection refused
Vianus le Fus
deadviannou at caramail.com
Mon Feb 23 14:09:13 EST 2004
Hello,
I'm a newbie to python so please don't be too hard with me :)
I'm encountering problems using socket connections in my programs. My goal is to create a small chat program
so I begin with the beginning : two really small apps, one for the server and one for the client.
The server is here to listen to one port and to resend its own data to the client that's connected to the port.
I have found two ways to test this : I compile the server with py2exe and launch it, then I can run the client
either running the module under IDLE or compiling it with py2exe.
And there's my problem : it works under IDLE (connection is set and the client receives its own data) but
connection fails when using the exe client.... it says "error 10061 : Connection refused". I don't have any
firewall nor other running programs that could block the socket, I really don't understand what's the difference
using py2exe or not !!
Please does someone know why the first method works and not the other ?
-----------------------------------------------------
Serveur.py
-----------------------------------------------------
import socket
HOST = '127.0.0.1'
PORT = 50007
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
data = conn.recv(1024)
if not data: break
conn.send(data)
conn.close()
-----------------------------------------------------
Client.py
-----------------------------------------------------
import errno
import socket
from Tkinter import *
import tkMessageBox
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.connected = 0
self.data = ''
self.pack()
self.createWidgets()
def createWidgets(self):
self.btn_quit = Button(self)
self.btn_quit["text"] = "QUIT"
self.btn_quit["fg"] = "red"
self.btn_quit["command"] = self.end
self.btn_quit.pack({"side": "left"})
self.btn_connect = Button(self)
self.btn_connect["text"] = "Connect",
self.btn_connect["command"] = self.connect
self.btn_connect.pack({"side": "right"})
def connect(self) :
try:
self.s.connect(('127.0.0.1', 50007))
self.connected = 1
except socket.error, msg:
tkMessageBox.showinfo(title='Connexion error', message='Can\'t connect \
to the server 127.0.0.1' + '\n' + str(errno.errorcode[msg[0]]))
if self.connected == 1 :
self.s.send('Hello, world')
self.data = self.s.recv(1024)
print 'Received', str(self.data)
tkMessageBox.showinfo(title='Data received !!', message=str(self.data))
def end(self) :
self.s.close()
self.quit()
# MAIN
app = Application()
app.mainloop()
-----------------------------------------------------
Plus simple, plus fiable, plus rapide : découvrez le nouveau Caramail - http://www.caramail.lycos.fr
More information about the Tutor
mailing list