[Tutor] Python and sockets

cecilwesterhof at xs4all.nl cecilwesterhof at xs4all.nl
Tue Aug 17 15:32:10 CEST 2004


> Theres a book out soon about python socket programming,
> http://www.compman.co.uk/cgi-win/browse.exe?ref=658967

I'll keep an eye out for it.


> Also, I find www.twistedmatrix.com much more useful for network programs,
> and Im still a python beginner.

Looks interesting.


> Regarding your problem, it'd probably be best if you could post some of
> the
> offending code ;)

Here is the program:
#!/bin/python

import socket;
import sys;
import Tkinter;

def Connect():
	global Connected, Socket;

	if( Connected == False ):
		Socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM);
		Socket.connect(('localhost', 4001));
		print "Connected to server";
		Connected = True;
	else:
		print "Allready connected to server";

def Disconnect():
	global Connected, Socket;

	if( Connected == True ):
		Socket.shutdown(2);
		Socket.close();
		Socket = "";
		print "Disconnected from server";
		Connected = False;
	else:
		print "Not connected to server";

def Exit():
	global Connected, Socket;

	if( Connected == True ):
		Socket.close();
	print "We zijn klaar";
	sys.exit();

def Receive():
	global Connected, Socket;

	if( Connected == True ):
		Received = Socket.recv(8192);
		print "Received: ", Received;
	else:
		print "Not connected to server";

Socket = "";
Connected = False;
Tkinter.Label(text = "Welcome!").pack();
Tkinter.Button(text = "Connect", command = Connect).pack();
Tkinter.Button(text = "Disconnect", command = Disconnect).pack();
Tkinter.Button(text = "Receive", command = Receive).pack();
Tkinter.Button(text = "Exit", command = Exit).pack();
Tkinter.mainloop();



More information about the Tutor mailing list