The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 20, 2019, 12:19:35 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: typeerror: an integer is required on python  (Read 10067 times)
nubie
Rookie
**
Offline Offline

Posts: 28


View Profile
« on: December 09, 2009, 07:45:25 PM »

Hi there, i've faced some issue about code that i get from some python book, i've just modified a little of that code :,
Code:
#multichat-multicast chat & whiteboard APP

from Tkinter import *
from socket import *
import cPickle, threading, sys

#each message is command + data
CMD_JOINED, CMD_LEFT, CMD_MSG, CMD_LINE, CMD_JOINRESP = range(5)
people = {} #key = (ipaddr.port), value = (name, color)

def sendMsg(msg):
sendSock.send(msg, 0)
def onQuit():
'User clicked quit Button'
sendMsg(chr(CMD_LEFT)) #Notify others that i'm leaving
root.quit()
def onMove(e):
'Called when LButton is down and mouse moves'
global lastLine, mx, my
canvas.delete(lastLine) #delete temp line
mx, my = e.x.e.y

#draw a new temp line
lastLine = canvas.create_line(dx, dy, mx, my, width = 2, fill="Black")
def onBDown(e):
'User pressed left mouse button'
global lastLine, dx, dy, mx, my
canvas.bind('<motion>', onMove) #start receiving some messages
dx, dy = e.x.e.y
mx, my = e.x.e.y
#draw a temporarily line
lastline = canvas.create_line(dx, dy, mx, my, width = 2, fill = "Black")
def onBUp(e):
'User released left mouse button'
canvas.delete(lastLine) #erase temporary line
canvas.unbind('<motion>') #no more move msgs. delete it

#send out the draw-a - line command
sendMsg(chr(CMD_LINE) + cPickle.dumps((dx, dy, e.x.e.y), 1))
def onEnter(foo):
'User hit the [enter] key'
sendMsg(chr(CMD_MSG)+entry.get())
entry.delete(0, END) #clear the entry widget
def setup(root):
'Creates the UI'
global msgs, entry, canvas
#the big window holding everybody messages
msgs = Text(root, width=60, height=20)
msgs.grid(row=0, col = 0, columnspan = 3)
#hookup the scrool bar to see oldmessage
s = Scrollbar(root, orient = VERTICAL)
s.config (command=msgs.yview)
msgs.config(yscrollcommand=s.set)
s.grid(row=0, col=3, sticky=N+S)
#where you type your message
entry = Entry(root)
entry.grid(row=1, col=0, columnspan=2, sticky=W+E)
entry.bind('<return>', onEnter)
entry.focus_set()

b = Button(root, text = 'Quit', command=onQuit)
b.grid(row = 1, col= 2)

#a place to draw
canvas = Canvas(root, bg="white")
canvas.grid(row = 0, col = 5)
#notify me of button press and release the message
canvas.bind('<ButtonPress-1>', onBDown)
canvas.bind('<ButtonRelease-1>', onBUp)
def msgThread(add, port, name):
'Listens for and process messages'
#create a listen socket
s = socket(AF_INET, SOCK_DGRAM)
s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
s.bind((" ", port))
#join the multicast group
s.setsockopt(SOL_IP, IP_ADD_MEMBERSHIP, inet_aton(addr)+inet_aton(''))
while 1:
#get a msg and strip of the command byte
msg.msgFrom = s.recvfrom(2048)
cmd, msg = ord(msg[0]), msg[1:]
if cmd == CMD_JOINED: #new joine
msgs.insert(END, '(%s joined the chat)\n' % msg)
#introduce myself
sendMsg(chr(CMD_JOINRESP) + cPickle.dumps((name, myColor), 1))
elif cmd == CMD_LEFT: #somebody left
who = people[msgFrom][0]
if who == name: #left, better quit
break
msgs.insert(END, '(%s left the chat)\n' % who, 'color_'+who)
elif cmd == CMD_MSG: #new message to display
who == people[msgFrom][0]
msgs.insert(END, who, 'color_%s' % who)
msgs.insert(END, ': %s\n' % msg)
elif cmd == CMD_LINE: # draw a line
dx, dy, ex, ey = cPickle.loads(msg)
canvas.create_line(dx, dy, ex, ey, width=2, fill = people[msgFrom][1])
elif cmd == CMD_JOINRESP: #introducing themselves
people[msgFrom] = cPickle.loads(msg)
who.color = people[msgFrom]
#create tag to draw text in their color
msgs.tag_configure('color_' + who, foreground=color)
#leave the multicast group
s.setsockopt(SOL_IP, IP_DROP_MEMBERSHIP, inet_aton(addr)+inet_aton(''))

if __name__ == '__main__':
argv = sys.argv[0]
if len(argv) < 3:
print 'Usage: ', argv[0], '<name> <color> [addr=<multicast address>] [port=<port>]'
sys.exit(1)
global name, addr, port, myColor
addr = '192.168.0.67' #default for ip address
port = '54321' #default port
name, myColor = argv[1:3]
for arg in argv[3:]:
if arg.startswith('addr= '):
addr = arg[len('addr= '):]
elif arg.startswith('port= '):
port = int(arg[len('port= '):])
#start up a thread to process messages
threading.Thread(target=msgThread, args=(addr, port, name)).start()

#this is the socket  over which w send the messages through
global sendSock
sendSock = socket(AF_INET, SOCK_DGRAM)
sendSock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
sendSock.connect((addr, port))

#don't let the packet die too soon
sendSock.setsockopt(SOL_IP, IP_MULTICAST_TTL, 2)
#create a TK window and create the GUI
root = Tk()
root.title('%s chatting on channel %s:%d' % (name, addr, port))
setup(root)

#join the chat
sendMsg(chr(CMD_JOINED) + name)
root.mainloop()
and i ran that code and here's some of that error:
Code:
Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Python254\lib\threading.py", line 486, in __bootstrap_inner
    self.run()
  File "C:\Python254\lib\threading.py", line 446, in run
    self.__target(*self.__args, **self.__kwargs)
  File "C:\Python254\code\chat_tk.py", line 75, in msgThread
    s.bind((" ", port))
  File "<string>", line 1, in bind
TypeError: an integer is required


Traceback (most recent call last):
  File "C:\Python254\code\chat_tk.py", line 127, in <module>
    sendSock.connect((addr, port))
  File "<string>", line 1, in connect
TypeError: an integer is required

What i need to make sure is, is it true the issue is just on the port that need use integer than string , but sorry i've forgot how to that on some function  D'oh!,
need assistance please, but i'm very curious about the module on python cause, for my personal opinion python very useful cause the plenty of the module that
help me make every tools what i need but how about if there's some error on the module itself, is there any way to help or i must call the creator of that module first Huh?.

Thank's a lot and need advise please  Praise
Logged
nop_90
Global Moderator
Lifer
*****
Offline Offline

Posts: 2203


View Profile
« Reply #1 on: December 10, 2009, 02:32:38 AM »

error is in this part

addr = '192.168.0.67' #default for ip address
   port = '54321' #default port
   name, myColor = argv[1:3]
   for arg in argv[3:]:
      if arg.startswith('addr= '):
         addr = arg[len('addr= '):]
      elif arg.startswith('port= '):
         port = int(arg[len('port= '):])

if you do not set port on command line the port value is set to '54321' which is a string
just change that to an integer value

Logged
isthisthingon
Global Moderator
Lifer
*****
Offline Offline

Posts: 2879



View Profile
« Reply #2 on: December 10, 2009, 11:12:01 AM »

Hop nop solved your issue, cuz I'm no Python guy Smiley
Logged

I would love to change the world, but they won't give me the source code.
nubie
Rookie
**
Offline Offline

Posts: 28


View Profile
« Reply #3 on: December 10, 2009, 08:18:51 PM »

Thank's a lot for your replies nop_90, so you mean the
port = '54321'
i must change the string to integer, with like this:
port = int('54321')

Is it what you mean ?

Thank's a lot and sorry for distrurb your time,


Regards,  Smiley
nubie
Logged
nop_90
Global Moderator
Lifer
*****
Offline Offline

Posts: 2203


View Profile
« Reply #4 on: December 11, 2009, 01:35:36 AM »

yep
port = 54321

it cool
Logged
nubie
Rookie
**
Offline Offline

Posts: 28


View Profile
« Reply #5 on: December 13, 2009, 10:46:23 PM »

Sorry again nop_90, but the problem of the integer has gone away and new error facing me again,
here's the error
Code:
Traceback (most recent call last):
  File "C:\Python254\code\chat_tk.py", line 134, in <module>
    setup(root)
  File "C:\Python254\code\chat_tk.py", line 49, in setup
    msgs.grid(row=0, col = 0, columnspan = 3)
  File "C:\Python254\lib\lib-tk\Tkinter.py", line 1869, in grid_configure
    + self._options(cnf, kw))
TclError: ambiguous option "-col": must be -column, -columnspan, -in, -ipadx, -ipady, -padx, -pady, -row, -rowspan, or -sticky
>>> Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Python254\lib\threading.py", line 486, in __bootstrap_inner
    self.run()
  File "C:\Python254\lib\threading.py", line 446, in run
    self.__target(*self.__args, **self.__kwargs)
  File "C:\Python254\code\chat_tk.py", line 75, in msgThread
    s.bind((" ", port))
  File "<string>", line 1, in bind
gaierror: (11001, 'getaddrinfo failed')
I'm used python 2.5.4 version, may i know what i must analyze when error have connection to the module file, like that error show there is something wrong on file
threading.py or this is just pure the faulting code of mine ?  Huh?  Need Help
I've been search on google and read some help file of threading module, but still yet wrong  D'oh!.

Thank's a lot.

Regards,
Nubie
Logged
nop_90
Global Moderator
Lifer
*****
Offline Offline

Posts: 2203


View Profile
« Reply #6 on: December 13, 2009, 11:03:56 PM »

The code is for "multicast" sockets.
http://wiki.python.org/moin/UdpCommunication

According to this there is problems with multicasting in python.
You could try replacing the bind with
s.bind((" ", port)) with s.bind(('',port))

You do not hear much about multicasting now days.
Logged
nubie
Rookie
**
Offline Offline

Posts: 28


View Profile
« Reply #7 on: December 14, 2009, 11:41:35 PM »

Sorry again nop_90, but the error coming again, after i look help on Tkinter and changes all col to column,
Code:
Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Python254\lib\threading.py", line 486, in __bootstrap_inner
    self.run()
  File "C:\Python254\lib\threading.py", line 446, in run
    self.__target(*self.__args, **self.__kwargs)
  File "chat_tk.py", line 102, in msgThread
    msgs.tag_configure('color_' + who, foreground=color)
  File "C:\Python254\lib\lib-tk\Tkinter.py", line 3090, in tag_configure
    return self._configure(('tag', 'configure', tagName), cnf, kw)
  File "C:\Python254\lib\lib-tk\Tkinter.py", line 1199, in _configure
    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
TclError: unknown color name "a"
And is there any tutorial or list error python code cause, everytime i've got facing an error and i ask to this forum you can always solve, but if i try to copy
the error thing to google to find the main issue hard to find same issue, or when i write code i must mastering first the all module on python and the rules of writing, as long as i faced an error cause the wrong types sequences, wrong function modules, etc.  Praise

Thank's a lot and sorry for this thing.

Regards,
Nubie
Logged
nop_90
Global Moderator
Lifer
*****
Offline Offline

Posts: 2203


View Profile
« Reply #8 on: December 15, 2009, 01:01:01 AM »

http://wiki.python.org/moin/TkInter

I have not used Tk with python.
Only Tk with perl.

Tk is a really old gui Smiley
If u going GUI route check out pyqt or wxpython
Logged
nop_90
Global Moderator
Lifer
*****
Offline Offline

Posts: 2203


View Profile
« Reply #9 on: December 15, 2009, 01:02:24 AM »

Also with python big advantage is interactive shell
check out
http://ipython.scipy.org/doc/manual/html/interactive/tutorial.html

So u can test little snippets of code etc
Logged
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!