LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-23-2010, 01:55 PM   #1
xskycamefalling
Member
 
Registered: May 2009
Posts: 43

Rep: Reputation: 15
Python socket question dealing with the ip address when its user entered


ok so i guess i should also state that im using Tkinter to do this..

ill post my code first and then break down the function that im having trouble with



Code:
import socket
from Tkinter import *
password = 'jon'



def pwdin():
    key = ent.get()
    if key != password:
       print 'Need to replace with window error or label error'
    else:
        pwdcheck.destroy()
        instructions()

def instructions():
    greeting = Label(root, text = 'Welcome')
    greeting.pack()
    connectButton.pack()
    getLogButton.pack()



#####################################################
#added this function to get ip address and port from#
#user to explain starting at line 1                 #
#create new window assign it to ipAddrWindow        #
#create text label so user knows what to do         #
#create entry for ip address                        #
#text for port                                      #
#entry box for port                                 #
#create ok button giving it the command socket have #
#not created this function yet                      #
#pack everything so it shows up                     #
#####################################################
def ip_grab():
    global iptext
    global porttext
    global ipAddrWindow

    ipAddrWindow = Toplevel()
    prompt = Label(ipAddrWindow, text = 'Enter IP Address')
    iptext = Entry(ipAddrWindow, bg = 'white')
    portpromt = Label(ipAddrWindow, text = 'Enter Port Number')
    porttext = Entry(ipAddrWindow, bg = 'white')
    okbut = Button(ipAddrWindow, text = "OK", command = socket)
    prompt.pack()
    iptext.pack()
    portpromt.pack()
    porttext.pack()
    okbut.pack()

def socket():
    import socket
    global ip, port, client_socket
    ip = iptext.get()
    port = porttext.get()
    ipAddrWindow.destroy()
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client_socket.connect((ip, port))

####################################################
#Create Main Window                                #
#Create Password Window                            #
#Create label promt password text                  #
#Create Entry Box for password window backrnd white#
#Pack all three widgits                            #
####################################################
root = Tk()
pwdcheck = Toplevel()
passtxt = Label(pwdcheck, text = 'Password')
ent = Entry(pwdcheck, bg = 'white', show = "*")
button = Button(pwdcheck, text = 'OK', command = pwdin)
ent.focus()
passtxt.pack()
ent.pack()
button.pack()
####################################################
#Buttons that will be packed after the password is #
#correctly input                                   #
####################################################
connectButton = Button(root, text = 'Connect', pady = 10, command = ip_grab)
getLogButton = Button(root, text = 'Get Log', pady = 10)
root.mainloop()
ok so my problem lies in the socket function that ive created..
the ip address is input into an entry box widgit and i think the variable type is auto set to string to be able to use variable.get() and set() functions.. so when i get the input from the entry box i think its storing it as a string
anyway the socket touple is expecting an integer ive tried int(varialbe) but this dosent seem to be working my guess is because of the . . . . in the ip address.. when i try to convert to int i get the following error msg

Code:
Traceback (most recent call last):
  File "<pyshell#15>", line 1, in <module>
    int(a)
ValueError: invalid literal for int() with base 10: '192.168.1.1'
just did that in the interpreter to show as example..

any other ideas?
 
Old 03-24-2010, 01:48 AM   #2
kakaka
Member
 
Registered: Sep 2003
Posts: 382

Rep: Reputation: 86
Actually, although the connect is expecting 2 values, the ip is supposed to be a string. It's the port that is supposed to be an int. The following seems to work for me without complaint:

client_socket.connect((ip, int(port) ))
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python IP Address socket problem with bind xskycamefalling Programming 5 02-14-2010 09:57 PM
python: socket with makefile Ephracis Programming 0 02-25-2009 08:57 AM
LXer: Dealing with Command Line Options in Python LXer Syndicated Linux News 0 12-04-2008 03:40 AM
Python socket: does not recieve anything Hewson Programming 4 03-28-2008 12:53 PM
Quick question dealing with XP Brother Michael General 2 07-15-2004 01:31 AM


All times are GMT -5. The time now is 08:12 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration