The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 23, 2019, 10:21:22 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Ask about Idle GUI python  (Read 5605 times)
nubie
Rookie
**
Offline Offline

Posts: 28


View Profile
« on: June 26, 2008, 09:43:21 PM »

Hi guys sory for disturbing your time, i'm very new in python so when i used Idle GUI and write like this:
 say = "HELLO"
 say[3]
so i save and i press f5 to run but when new window showed up that not show anything, but if i used python
command line that script can showed up.
And also i want to ask some question again when i copy paste some code python from internet and i paste it to Idle,
i save and i running it that script,  this is the code i copied:
   
============================================================
#   A simple "chat" server.  Creates a server on port 4000.
#   Users telnet to your machine, port 4000, and can chat with each
#   other.  Use "quit" to disconnect yourself, and "shutdown" to
#   shut down the server.  Requires sockets.
#
#   7/31/96   J. Strout      http://www.strout.net/

# import needed modules:

from socket import *      # get sockets, for well, sockets
import string            # string functions
import time               # for sleep(1) function

# define global variables

HOST = ''            # Symbolic name meaning the local host
PORT = 4000            # Arbitrary non-privileged server
endl = "\r\n"         # standard terminal line ending

userlist = []         # list of connected users
done = 0            # set to 1 to shut this down

kAskName = 0         # some constants used to flag
kWaitName = 1         #   the state of each user
kOK = 2


# class to store info about connected users

class User:
   def __init__(self):
      self.name = ""
      self.addr = ""
      self.conn = None
      self.step = kAskName

   def Idle(self):   
      if self.step == kAskName: self.AskName()

   def AskName(self):
      self.conn.send("Name? ")
      self.step = kWaitName

   def HandleMsg(self, msg):
      print "Handling message: ",msg
      global userlist
      
      # if waiting for name, record it
      if self.step == kWaitName:
         # try to trap garb initiall sent by some telnets:
         if len(msg) < 2 or msg=="#": return
         print "Setting name to: ",msg
         self.name = msg
         self.step = kOK
         self.conn.send("Hello, "+self.name+endl)
         broadcast(self.name+" has connected."+endl)
         return

      # check for commands
      if msg == "quit":
         broadcast(self.name+" has quit.\n")
         self.conn.close()
         userlist.remove(self)
         return

      # otherwise, broadcast msg
      broadcast( self.name+": "+msg+endl )


# routine to check for incoming connections

def pollNewConn():
   try:
      conn, addr = s.accept()
   except:
      return None
   print "Connection from", addr
   conn.setblocking(0)
   user = User();
   user.conn = conn
   user.addr = addr
   return user


# routine to broadcast a message to all connected users

def broadcast(msg):
   for u in userlist:
      u.conn.send(msg)


# MAIN PROGRAM


# set up the server

s = socket(AF_INET, SOCK_STREAM)
s.bind(HOST, PORT)
s.setblocking(0)
s.listen(1)
print "Waiting for connection(s)..."

# loop until done, handling connections and incoming messages

while not done:
   time.sleep(1)      # sleep to reduce processor usage
   u = pollNewConn()   # check for incoming connections
   if u:
      userlist.append(u)
      print len(userlist),"connection(s)"

   for u in userlist:   # check all connected users
      u.Idle()
      try:
         data = u.conn.recv(1024)
         data = filter(lambda x: x>=' ' and x<='z', data)
         data = string.strip(data)
         if data:
            print "From",u.name,': ['+data+']'
            u.HandleMsg(data)
            if data == "shutdown": done=1
      except:
         pass

for u in userlist:
   u.conn.close()
=========================================================
when i run that code it showed error "Traceback (most recent call last):
  File "E:\TEST\chat_server.py", line 98, in <module>
    s.bind(HOST, PORT)
  File "<string>", line 1, in bind
TypeError: bind() takes exactly one argument (2 given)"
and sometimes i wrote a code in idle and i running the script it sometimes showed error "Unexpected indent".
I'm sorry about this question, i hope i can get some explanation and help from you guys.
Thank's a lot.  Praise

Logged
nop_90
Global Moderator
Lifer
*****
Offline Offline

Posts: 2203


View Profile
« Reply #1 on: June 27, 2008, 02:21:21 AM »

the bug is here i believe
s.bind(HOST, PORT)

http://docs.python.org/lib/socket-objects.html
bind(     address)
    Bind the socket to address. The socket must not already be bound. (The format of address depends on the address family -- see above.) Note: This method has historically accepted a pair of parameters for AF_INET addresses instead of only a tuple. This was never intentional and is no longer available in Python 2.0 and later.

Address should be a tuple

so it should be s.bind((HOST,PORT)) i believe
Logged
nubie
Rookie
**
Offline Offline

Posts: 28


View Profile
« Reply #2 on: June 30, 2008, 09:17:29 PM »

Thanks for your reply Sir nop_90, i had try that and it's work now, but sir nop_90 can i ask one question,
how can you try to add some () in to the script (HOST, PORT) cause i don't get any idea about that.

Thank you so much and i'm sorry for disturbing your time.
Logged
nop_90
Global Moderator
Lifer
*****
Offline Offline

Posts: 2203


View Profile
« Reply #3 on: July 01, 2008, 02:02:33 AM »

in python there are 2 types of list structures
tupes as in
A=(1,2,3)
or lists
A=[1,2,3]
the difference between the 2 is lists can be changed but tuples can not be. (immutable is the term i believe)
so prior to version 2.0
s.bind(HOST, PORT)
but after version 2.0 (because of different addressing schemes)
they say
s.bind(address) where address is a tuple in form (host,port)

so doing
address = (host,port)
s.bind(address)
is same as
s.bind((host,port))
Logged
nubie
Rookie
**
Offline Offline

Posts: 28


View Profile
« Reply #4 on: July 01, 2008, 03:57:37 AM »

Thanks Sir nop_90, finally i understand that.
Thank you so much for your explanation that's very help me.

Logged
arms
Expert
****
Offline Offline

Posts: 235



View Profile
« Reply #5 on: July 08, 2008, 08:19:41 PM »

mr nubie you should check out some of the other python IDEs.

free
eclipse with pydev (a pain to install and hogs memory but fully featured)
http://www.eclipse.org/
http://pydev.sourceforge.net/

geany (if you are on linux)
Code:
sudo apt-get install geany

SPE IDE
http://pythonide.blogspot.com/

NOT free
wingide (by far the most fully featured)
komodo


and ide like wingide or eclipse+pydev is good way to learn because you have in-editor documentation, help with syntax errors etc.
Logged
nubie
Rookie
**
Offline Offline

Posts: 28


View Profile
« Reply #6 on: July 09, 2008, 04:12:27 AM »

Hi Sir arms i had visited that site but i want to ask some question about i used python 2.5.2 can i still use this
for install pydev or i must downgrade the version python and in site eclipse i saw that in eclipse site now use
package eclipse 3.4 while in the requirement of pydev use eclipse 3.3.
Thank you for helping me and give me advices, and sorry for this stupid question.
Logged
arms
Expert
****
Offline Offline

Posts: 235



View Profile
« Reply #7 on: July 09, 2008, 09:02:16 AM »

yes eclise + pydev is a bit of a pain.

install the latest version of eclipse.
then follow the instructions here:
http://www.fabioz.com/pydev/manual_101_root.html

i warn you there is a lot of configuration and setting up projects.

as an alternative, if you have too much trouble installing, try SPE IDE:
http://developer.berlios.de/project/showfiles.php?group_id=4161
Logged
nop_90
Global Moderator
Lifer
*****
Offline Offline

Posts: 2203


View Profile
« Reply #8 on: July 09, 2008, 03:08:32 PM »

just to add little more to arms.
you could also check out
http://drpython.sourceforge.net/
spe
http://pythonide.stani.be/ https://developer.berlios.de/projects/python/
Logged
arms
Expert
****
Offline Offline

Posts: 235



View Profile
« Reply #9 on: July 09, 2008, 08:00:40 PM »

i didn't know drpython, installing it now.
i use wingide but its expensive (more than free).
im REALLY liking geany.
it loads up fast, uses little resources, handles python as nice as it does php or html.
the only thing it's missing for me is python autocomplete.
fucking thing loads faster than that piece of shit gedit.
Logged
nop_90
Global Moderator
Lifer
*****
Offline Offline

Posts: 2203


View Profile
« Reply #10 on: July 09, 2008, 09:10:50 PM »

geany looks nice Smiley
if i have to do any python code in the future i will give it a try.
Logged
nubie
Rookie
**
Offline Offline

Posts: 28


View Profile
« Reply #11 on: July 10, 2008, 05:12:45 AM »

Thank you so much for all of you helps and advices, i had download all that tools maybe i can try it at my home
and can i post some code to this forum and ask you guys to give me some advices about code that i write(for now
i still follow examples from the ebook and just modified after i tried that code) ?.
You guys are so amazing, thank's a lot  Smiley
Logged
cd1
Rookie
**
Offline Offline

Posts: 16


View Profile
« Reply #12 on: September 05, 2008, 11:40:59 PM »

Check out Komodo from ActiveState. It is a pretty kickass IDE for windows or linux that does autocomplete, syntax checking, debugging, etc.  They offer a free 30 day trial, but you can get a cracked version from the usual sources. It also works well w/ php, ruby, c, etc.
Logged

No links in signatures please
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!