Join us at IRC!
The important thing is not to stop questioning. - Albert Einstein
Friday, May 25, 2012
Navigation
Members Online
Total Online: 31
Web Spiders: 15
Guests Online: 30
Members Online: 1

Registered Members: 70209
Newest Member: KalareShou
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

Python sockets

Demons Halo
Member



Posts: 261
Location: Sweden
Joined: 26.03.09
Rank:
Hacker Level 1
Posted on 18-01-10 18:20
YO again!

I've just started reading about socket programming in python.

#Python Server
import socket

addr = ("localhost", 3737)
buf = 1024

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(addr)
while True:
try:
data, address = s.recvfrom(buf)
print str(data) + "______" + str(address)
s.close()
break
except socket.error:
print "shit happens"
s.close()
break




#Python Client
import socket

addr = ("localhost", 3737)
buf = 1024

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.sendto("MOHAHAHA IT'S ALIVE!!!", addr)
s.close()



Now when I run either of the scblockedripts I get the following error:

error: [Errno 10049] The requested address is not valid in its context


This should not happen :/

Google says:
This normally results from an attempt to bind to an address that is not valid for the local computer.


which should not be the case -_-

any suggestions? :P

Edit: It must be my firewall. There is nothing wrong with the freaking code -_-

Edited by Demons Halo on 18-01-10 21:45
base_dropper@hotmail.com www.demonshalo.com
Author

RE: Python sockets

Demons Halo
Member



Posts: 261
Location: Sweden
Joined: 26.03.09
Rank:
Hacker Level 1
Posted on 18-01-10 21:39
thnx for the replay mosh =)

fact is that my code works if i use "localhost" as the address. but once I try to reach another computer within the LAN, I get an error.
This is a firewall issue right? or do I need to do something different when I code LAN applications?

Edited by Demons Halo on 18-01-10 21:39
base_dropper@hotmail.com www.demonshalo.com
Author

RE: Python sockets

Demons Halo
Member



Posts: 261
Location: Sweden
Joined: 26.03.09
Rank:
Hacker Level 1
Posted on 18-01-10 21:45
MoshBat wrote:
Works perfectly for me, though your code spat out errors.
Firewall/OS?


well using the code above (I updated the code fields) gave no errors on "localhost". But when I replace the "localhost" with "remote ip", the errors starts popping up :/

I'm using Win7 x86 with built in firewall :p nothing fancy xD
base_dropper@hotmail.com www.demonshalo.com
Author

RE: Python sockets

Demons Halo
Member



Posts: 261
Location: Sweden
Joined: 26.03.09
Rank:
Hacker Level 1
Posted on 18-01-10 22:02
ooooooh I see!!!

you can appearently not bind an external/remote ip. so what you have to do is leave the address field empty. I'll post the code here in case anyone runs into a similar problem:

#Python Server, running on my stationary PC
import socket

addr = ("", 3737) #Notice that there is no given address
buf = 1024

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #Using UDP!
s.bind(addr)

while True:
try:
data, address = s.recvfrom(buf) #recieving info
print str(data) + " " + str(address)
except socket.error:
print "shit happens"
s.close()
break



#Python Client, running on my laptop
import socket

addr = ("192.168.0.64", 3737) #my stationarys LAN address
buf = 1024

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #Using UDP!
s.sendto("I'm here to serve you master!!!", addr)
s.close()


Also, you must make sure that you OS firewall allows connections to be made by/to the server!

Edited by Demons Halo on 18-01-10 22:03
base_dropper@hotmail.com www.demonshalo.com
Author

RE: Python sockets

wolfmankurd
Member



Posts: 1519
Location: UK
Joined: 30.05.05
Rank:
God
Posted on 18-01-10 22:10
or 0.0.0.0 should work


BY READING MY POST, YOU ACCEPT IT AS IS AND AGREE TO MY DISCLAIMER OF ALL WARRANTIES, EXPRESS OR IMPLIED, AS WELL AS DISCLAIMERS OF ALL LIABILITY, DIRECT, INDIRECT, CONSEQUENTIAL OR INCIDENTAL, THAT MAY ARISE FROM THE USE OF THIS (MIS)INFORMATION.


Widowmakr@hotmail.com http://LetsHackStuff.com
Author

RE: Python sockets

Demons Halo
Member



Posts: 261
Location: Sweden
Joined: 26.03.09
Rank:
Hacker Level 1
Posted on 18-01-10 22:21

or 0.0.0.0 should work

thnx, good to know =)

MoshBat wrote:
Wait, you were putting the remote machine's IP as the server's host?


yes ;$ I thought I had to bind that remote ip to the server in order to be able to accept incoming data. apparently I was wrong since the server is listening for whoever connects!

Also I noticed 1 thing. The server is listening to port 3737, and the client sends the data to that port. Then why is it that I get this:

I'm here to serve you master!! ('192.168.0.66', 65011)

notice that 192.168.0.66 (my laptop) connected to my server 192.168.0.64. Although the connection was not made @ port 3737, instead they used 65011.
Is there a explanation for that or am I being stupid once again? :P
base_dropper@hotmail.com www.demonshalo.com
Author

RE: Python sockets

techb
Member



Posts: 384
Location:
Joined: 15.02.09
Rank:
Hacker Level 2
Posted on 24-02-10 19:07
the port it is listening on is just that. It listens for a connection, once one is established it is passed to another port for communication or "talk" between the server and client.
kbcarte.wordpress.com
Author

RE: Python sockets

stealth-
Member



Posts: 999
Location: Eh?
Joined: 10.04.09
Rank:
God
Posted on 25-02-10 00:51
techb wrote:
the port it is listening on is just that. It listens for a connection, once one is established it is passed to another port for communication or "talk" between the server and client.


Are you sure that's right? I was always under the impression that the client does not open a connection on it's 3737, to avoid another application that may be using that port, so it instead binds the connection to it's own, much higher, random, port and communicates with port 3737 on the server.

The server talks on 3737 the whole time, which is why only one client can be connected (at least in TCP) and to have multiple clients you need asynchronus or threaded connections.

That was at least what I've been told, I could just be shooting out random blabber.


The irony of man's condition is that the deepest need is to be free of the anxiety of death and annihilation; but it is life itself which awakens it, and so we must shrink from being fully alive.
http://www.stealth-x.com
http://www.stealth-x.com
Author

RE: Python sockets

define
Member

Posts: 201
Location:
Joined: 13.12.08
Rank:
Moderate
Warn Level: 1
Posted on 25-02-10 01:41
stealth- wrote:
I was always under the impression that the client does not open a connection on it's 3737, to avoid another application that may be using that port, so it instead binds the connection to it's own, much higher, random, port and communicates with port 3737 on the server.

The server talks on 3737 the whole time, which is why only one client can be connected (at least in TCP) and to have multiple clients you need asynchronus or threaded connections.

That was at least what I've been told, I could just be shooting out random blabber.

This is right.

Here's a reference link for further reading:
http://en.wikipedia.org/wiki/Transmission_Control_Protocol#TCP_ports


If you need to contact me, send me a PM. I will read and/or respond in time.
Author

RE: Python sockets

techb
Member



Posts: 384
Location:
Joined: 15.02.09
Rank:
Hacker Level 2
Posted on 25-02-10 03:26
I guess my impression was wrong. Good to know though. I'm only in my first year of networking classes. So try not to go TOO hard on me lol. We're just now going over subnetting, joy....
kbcarte.wordpress.com
Author

RE: Python sockets

stealth-
Member



Posts: 999
Location: Eh?
Joined: 10.04.09
Rank:
God
Posted on 25-02-10 04:08
techb wrote:
I guess my impression was wrong. Good to know though. I'm only in my first year of networking classes. So try not to go TOO hard on me lol. We're just now going over subnetting, joy....


pfft, try not to go too hard on you? I took it as far as to say "Are you sure that's right?", I'm not sure I can get nicer than that :p


The irony of man's condition is that the deepest need is to be free of the anxiety of death and annihilation; but it is life itself which awakens it, and so we must shrink from being fully alive.
http://www.stealth-x.com
http://www.stealth-x.com
Guest
Username

Password

Remember Me


Bookmark This Page
Affiliates
Adverts

 

 

Links
By using, viewing or obtaining any information contained on this site, you agree to the disclaimer.

© HellBound Hackers 2008- 2009. Since 3rd December 2004.