LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 02-12-2010, 05:06 PM   #1
xskycamefalling
Member
 
Registered: May 2009
Posts: 43

Rep: Reputation: 15
Question Python IP Address socket problem with bind


Ok so the error msg i get is

Code:
serversocket.bind((ip1, 21))
File "<string>", line 1, in bind
error: [Errno 10049] The requested address is not valid in its context

the issue is im not using the classic method of
Code:
host = socket.gethostname()
ip = socket.gethostbyname(host)
instead im getting the physical ip using
Code:
site = urllib.urlopen('http://checkip.dyndns.org').read()
ip1 = re.search("(\d+\.\d+\.\d+\.\d+)", site).group()
this gives me the above message during the socket.bind method and im unsure as to why.. is this just not recognized as one of the ip addresses of the computer? can it only run off the 192.168.x.x or 10.x.x.x
Code:
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#get ip Address
site = urllib.urlopen('http://checkip.dyndns.org').read()
#use regex to get just IP
ip1 = re.search("(\d+\.\d+\.\d+\.\d+)", site).group()
#bind socket to ip and port
serversocket.bind((ip1, 21))
#start listening for client requests
serversocket.listen(1)
clientsocket, address = serversocket.accept()
 
Old 02-12-2010, 06:21 PM   #2
irmin
Member
 
Registered: Jan 2010
Location: the universe
Distribution: Slackware (modified), Slackware64 (modified), openSuSE (modified)
Posts: 342

Rep: Reputation: 62
Are you behind a router? In this case you cannot bind to its IP address, because it is not one of your IP addresses. You can bind to all IP Adresses by binding to 0.0.0.0.

If you are behind a router and want to provide a service on the internet you have to configure it to forward the specific ports.
 
Old 02-12-2010, 07:01 PM   #3
xskycamefalling
Member
 
Registered: May 2009
Posts: 43

Original Poster
Rep: Reputation: 15
yes im behind a router..
the port is forwarded i mean ive tried different higher ports and Forwarded them i know that 21 and 22 are open basically i want to be able to connect to another computer on a different network and send and receive a file.
it works fine using the local ip if its within my network but i want to be able to have access outside..
thus i grabbed the other ip..

would the binding to 0.0.0.0 allow access from the outside world?
 
Old 02-12-2010, 07:33 PM   #4
irmin
Member
 
Registered: Jan 2010
Location: the universe
Distribution: Slackware (modified), Slackware64 (modified), openSuSE (modified)
Posts: 342

Rep: Reputation: 62
If your router is configured correctly, binding to 0.0.0.0 should allow you to access from outside. If you enable port forwarding, the data packet for the ports will be sent to your computer as there were no router in between.

But you do not need to bind to 0.0.0.0 just bind to your local network IP.
 
1 members found this post helpful.
Old 02-13-2010, 02:59 AM   #5
Web31337
Member
 
Registered: Sep 2009
Location: Russia
Distribution: Gentoo, LFS
Posts: 399
Blog Entries: 71

Rep: Reputation: 65
Thanks xskycamefalling for this code, occasionally looked through this thread, I was just about to make external IP obtain script with python Information right in time.
 
Old 02-14-2010, 09:57 PM   #6
xskycamefalling
Member
 
Registered: May 2009
Posts: 43

Original Poster
Rep: Reputation: 15
Hmm ill have to give it a try Took a day off from it let u know what i get in a bit..
 
  


Reply



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
unix-socket error:bind: Address already in use sweetytweety Linux - Newbie 2 05-12-2008 01:13 AM
can not bind socket to address terrormaniak Linux - Server 1 09-14-2007 01:16 PM
can not bind socket to address terrormaniak Linux - Software 0 09-12-2007 03:00 PM
cannot bind to socket: address already in use exfacior Linux - Wireless Networking 0 09-27-2004 12:34 PM
Help me with this socket address bind program. rnice Linux - Networking 0 09-17-2004 07:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration