| Author |
Python sockets |
Demons Halo
Member

Posts: 261
Location: Sweden
Joined: 26.03.09 Rank: Hacker Level 1 |
|
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 |
|
| Author |
RE: Python sockets |
Demons Halo
Member

Posts: 261
Location: Sweden
Joined: 26.03.09 Rank: Hacker Level 1 |
|
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 |
|
| Author |
RE: Python sockets |
Demons Halo
Member

Posts: 261
Location: Sweden
Joined: 26.03.09 Rank: Hacker Level 1 |
|
|
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 nothing fancy xD |
|
| Author |
RE: Python sockets |
Demons Halo
Member

Posts: 261
Location: Sweden
Joined: 26.03.09 Rank: Hacker Level 1 |
|
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 |
|
| Author |
RE: Python sockets |
wolfmankurd
Member

Posts: 1519
Location: UK
Joined: 30.05.05 Rank: God |
|
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.

|
|
| Author |
RE: Python sockets |
Demons Halo
Member

Posts: 261
Location: Sweden
Joined: 26.03.09 Rank: Hacker Level 1 |
|
|
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
|
|
| Author |
RE: Python sockets |
techb
Member

Posts: 384
Location:
Joined: 15.02.09 Rank: Hacker Level 2 |
|
|
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. |
|
| Author |
RE: Python sockets |
stealth-
Member

Posts: 999
Location: Eh?
Joined: 10.04.09 Rank: God |
|
|
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 |
|
| Author |
RE: Python sockets |
define
Member
Posts: 201
Location:
Joined: 13.12.08 Rank: Moderate Warn Level: 1
|
|
|
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 |
|
|
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.... |
|
| Author |
RE: Python sockets |
stealth-
Member

Posts: 999
Location: Eh?
Joined: 10.04.09 Rank: God |
|
|
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 
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 |
|