Re: [Tutor] close failed in file object destructor:

2011-10-18 Thread Navneet

Hi,

I am trying to search a list for prime numbers but it's throwing me an 
error at line no.25.

I am not able to figure what exactly is the prob
ne help ??? Error is this:
$ python "prime1 - Copy.py"
Unhandled exception in thread started by
Traceback (most recent call last):
  File "prime1 - Copy.py", line 25, in findPrime
close failed in file object destructor:
Error in sys.excepthook:

program is below: Numberlist is number range from 1..1000


import sys
import threading
import thread
import time

class FindPno():

c = []
f = open("A;\Numberlist.txt")
for i in f:
c.append(i)
f.close()
##print len(c)
##Thread should start here
def __init__(self):
thread.start_new_thread(self.findPrime,(1,))

def findPrime(self,tid):
global tlock
##print "I am here"
tlock = thread.allocate_lock()
##print "I am here"
tlock.acquire()
##print "I am here"
for i1 in range(len(c)):   ##this is the 25th line
for i2 in range(2,int(c[i1])):

if int(c[i1]) == 1:
print "I am here"
tlock.release()
break
if int(c[i1]) == 2:
print c
print "I am here"
tlock.release()
break
rem = int(c[i1])%i2
if rem == 0:
print "I am here"
tlock.release()
break
if i2 == int(c[i1])-1:
print int(c[i1]), "This is the Thread",tid
print "I am here"
tlock.release()

tlock.release()

if __name__ == '__main__':
a = FindPno()

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Socket Programming

2012-01-26 Thread Navneet

Hi,

I am trying to create a chat program.(Programs are attached)
Please find the code below, where I am having the problem.

def run( self ):
while 1:
print "Hello There "
print self.descriptors
# Await an event on a readable socket descriptor
(sread, swrite, sexc) = select.select( self.descriptors, 
[], [] )

print sexc
# Iterate through the tagged read descriptors
print sread
print "Hello There 1 "
for sock in sread:


For this I am getting the output as below:
bash-3.1$ python Server.py
Enter the Port:...9009
ChatServer started on port 9009
Hello There 
[]


But it is not printing the value of sread or "Hello There 1 !"





import socket
import select


class ChatServer:
def __init__( self, port ):
self.port = port;
self.srvsock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
self.srvsock.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 )
self.srvsock.bind( ("", port) )
self.srvsock.listen( 5 )
self.descriptors = [self.srvsock]
print 'ChatServer started on port %s' % port
def run( self ):
while 1:
print "Hello There "
print self.descriptors
# Await an event on a readable socket descriptor
(sread, swrite, sexc) = select.select( self.descriptors, [], [] )
print sexc 
# Iterate through the tagged read descriptors
print sread
print "Hello There 1 "
for sock in sread:
# Received a connect to the server (listening) socket
if sock == self.srvsock:
self.accept_new_connection()
else:
# Received something on a client socket
str = sock.recv(100)
# Check to see if the peer socket closed
if str == '':
host,port = sock.getpeername()
str = 'Client left %s:%s\r\n' % (host, port)
self.broadcast_string( str, sock )
sock.close()
self.descriptors.remove(sock)
else:
host,port = sock.getpeername()
newstr = '[%s:%s] %s' % (host, port, str)
self.broadcast_string( newstr, self.srvsock )

def accept_new_connection( self ):
newsock, (remhost, remport) = self.srvsock.accept()
self.descriptors.append( newsock )
newsock.send("You're connected to the Python chatserver\r\n")
str = 'Client joined %s:%s\r\n' % (remhost, remport)
self.broadcast_string( str, newsock )

def broadcast_string( self, str, omit_sock ):
for sock in self.descriptors:
if sock != self.srvsock and sock != omit_sock:
sock.send(str)
print str,

if __name__ == '__main__':

portno = int(raw_input("Enter the Port:..."))
s = ChatServer(portno)
s.run()

from Tkinter import *
import tkMessageBox
import socket
import logging
import sys, time
import pickle
import thread



class ClientChat():
def __init__(self,serverport):
self.counter = 0

self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

self.host = 'localhost' # server address
self.port = serverport # server port 

# connect to server
self.s.connect((self.host, self.port))

self.chatclient = Tk()

self.chatclient["padx"] = 200
self.chatclient["pady"] = 200

self.chatclient.title("Client")

self.f1 = Frame(self.chatclient,width = 100, height = 10)
self.f1.pack(side = TOP)

self.f2 = Frame(self.chatclient,width = 100, height = 10)
self.f2.pack(side = BOTTOM)


self.recvmsg = Text(self.f1, width = 100, height = 10)
self.sb = Scrollbar(self.f1)
self.sb.pack(side = RIGHT, fill = Y)

self.sb.config(command = self.recvmsg.yview)
self.recvmsg.config(yscrollcommand=self.sb.set)
self.recvmsg.pack(padx = 10, pady =10)

self.sendmsg = Text(self.f2, width = 100, height = 5)
self.sendmsg.pack(padx = 10, pady =20)

self.sendbutton =Button(self.f2, width = 5, height = 2, text = "Send", 
command = self.senddata)
self.sendbutton.pack(side = LEFT, padx = 10, pady = 10)

self.sendbutton =Button(self.f2, width = 5, height = 2, text = "Exit", 
command = self.exitchat)
self.sendbutton.pack(side = RIGHT, padx = 10, pady = 10)

self.chatclient.mainloop()


def senddata(self):
self.k = self.sendmsg.get(1.0, END)
self.sendmsg.delete(1.0, END)
self.s.send(self.k)

if self.counter == 0:
thread.start_new_thread(self.recvdata,())
##if self.k == '':
##   

Re: [Tutor] Socket Programming

2012-01-27 Thread Navneet

On 1/26/2012 4:22 PM, Navneet wrote:

Hi,

I am trying to create a chat program.(Programs are attached)
Please find the code below, where I am having the problem.

def run( self ):
while 1:
print "Hello There "
print self.descriptors
# Await an event on a readable socket descriptor
(sread, swrite, sexc) = select.select( self.descriptors, 
[], [] )

print sexc
# Iterate through the tagged read descriptors
print sread
print "Hello There 1 "
for sock in sread:


For this I am getting the output as below:
bash-3.1$ python Server.py
Enter the Port:...9009
ChatServer started on port 9009
Hello There 
[]


But it is not printing the value of sread or "Hello There 1 !"






Hello All,

One more thing I want to add here is, I am trying to create the GUI 
based chat server.(Attached the programs.)

The concept is something like this:
I will start a server on a particular port and different Clients can 
interact with each other using that (more like a chat room )
Now I want to add the GUI part in a different module and want to call 
that module from client program.

But while executing the client1.py I am getting below error :



bash-3.1$ python Client1.py
Enter the server address:...9009
Traceback (most recent call last):
  File "Client1.py", line 53, in 
c = ClientChat(serverport)
  File "Client1.py", line 24, in __init__
gui.callGui()
  File "a:\FedEx\Exp\ClientGui.py", line 37, in callGui
sendbutton =Button(f2, width = 5, height = 2, text = "Send", 
command = C.ClientChat.senddata())
TypeError: unbound method senddata() must be called with ClientChat 
instance as first argument (got nothing instead)













from Tkinter import *
import tkMessageBox
import socket
import logging
import sys, time
import pickle
import thread
import ClientGui as gui



class ClientChat():
def __init__(self,serverport):
self.counter = 0

self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

self.host = 'localhost' # server address
self.port = serverport # server port 

# connect to server
self.s.connect((self.host, self.port))

gui.callGui()


def senddata(self):
self.k = gui.callGui.sendmsg.get(1.0, END)
gui.callGui.sendmsg.delete(1.0, END)
self.s.send(self.k)

if self.counter == 0:
thread.start_new_thread(self.recvdata,())
##if self.k == '':
##thread.exit()
##self.s.close()
##self.chatclient.destroy()

def exitchat():
self.s.close()
self.chatclient.destroy()

def recvdata(self):
while 1:
self.v = self.s.recv(1024) 
gui.callGui.recvmsg.insert(END,self.v)



if __name__ == '__main__':

serverport = int(raw_input("Enter the server address:..."))
c = ClientChat(serverport)


from Tkinter import *
import tkMessageBox
import socket
import logging
import sys, time
import pickle
import thread
import Client1 as C


def callGui():
chatclient = Tk()

chatclient["padx"] = 200
chatclient["pady"] = 200

chatclient.title("Client")

f1 = Frame(chatclient,width = 100, height = 10)
f1.pack(side = TOP)

f2 = Frame(chatclient,width = 100, height = 10)
f2.pack(side = BOTTOM)


recvmsg = Text(f1, width = 100, height = 10)
sb = Scrollbar(f1)
sb.pack(side = RIGHT, fill = Y)

sb.config(command = recvmsg.yview)
recvmsg.config(yscrollcommand=sb.set)
recvmsg.pack(padx = 10, pady =10)

sendmsg = Text(f2, width = 100, height = 5)
sendmsg.pack(padx = 10, pady =20)

sendbutton =Button(f2, width = 5, height = 2, text = "Send", command = 
C.ClientChat.senddata())
sendbutton.pack(side = LEFT, padx = 10, pady = 10)

sendbutton =Button(f2, width = 5, height = 2, text = "Exit", command = 
C.ClientChat.exitchat())
sendbutton.pack(side = RIGHT, padx = 10, pady = 10)

chatclient.mainloop()
import socket
import select


class ChatServer:
def __init__( self, port ):
self.port = port;
self.srvsock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
self.srvsock.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 )
self.srvsock.bind( ("localhost", port) )
self.srvsock.listen( 5 )
self.descriptors = [self.srvsock]
print 'ChatServer started on port %s' % port
def run( self ):
while 1:
# Await an event on a readable socket descriptor
sread, swrite, sexc = select.select( self.descriptors, [], [],5 )
# Iterate through the tagged read descriptors
for sock in sread:

Re: [Tutor] Socket Programming

2012-01-29 Thread Navneet

On 1/27/2012 10:13 PM, Steven D'Aprano wrote:

Navneet wrote:

One more thing I want to add here is, I am trying to create the GUI 
based chat server.(Attached the programs.)



Please do not send large chunks of code like this, unless asked. 
Instead, you should try to produce a minimal example that demonstrates 
the problem. It should be:


* short (avoid code which has nothing to do with the problem)

* self-contained (other people must be able to run it)

* correct (it must actually fail in the way you say it fails)

See here for more: http://sscce.org/


In cutting your code down to a minimal example, 9 times out of 10 you 
will solve your problem yourself, and learn something in the process.




bash-3.1$ python Client1.py
Enter the server address:...9009
Traceback (most recent call last):
  File "Client1.py", line 53, in 
c = ClientChat(serverport)
  File "Client1.py", line 24, in __init__
gui.callGui()
  File "a:\FedEx\Exp\ClientGui.py", line 37, in callGui
sendbutton =Button(f2, width = 5, height = 2, text = "Send", 
command = C.ClientChat.senddata())
TypeError: unbound method senddata() must be called with ClientChat 
instance as first argument (got nothing instead)



This one is easy. You need to initialize a ClientChat instance first. 
This may be as simple as:


command = C.ClientChat().senddata

although I'm not sure if ClientChat requires any arguments.

Note that you call the ClientChat class, to create an instance, but 
you DON'T call the senddata method, since you want to pass the method 
itself as a callback function. The button will call it for you, when 
needed.





Thanks for the clarification and telling me about SSCCE :)

But just a simple thing,,, Can I call a method of another module while 
creating a GUI.


For example
 C = Tk()
.(Some more lines)
self.sendbutton =Button(self.f2, width = 5, height = 2, text = "Send", 
command = .)

self.sendbutton.pack(side = LEFT, padx = 10, pady = 10)
.(Some more lines)
C.mainloop()


Because I am getting stuck in a loop. The client is keep on connecting 
to server without creating a GUI.












___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor