views:

292

answers:

2
+1  Q: 

Python +sockets

Hello, i have to create connecting server<=>client. I use this code: Server:

import socket

HOST = 'localhost'
PORT = 50007      
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
    data = conn.recv(1024)
    if not data: break
    conn.send(data)
conn.close()

Client:

import socket

HOST = 'localhost'   
PORT = 50007             
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.send('Hello, world')
data = s.recv(1024)
s.close()
print 'Received', repr(data)

It works fine! But if server is created on the computer which hasn't router. If u have router, before server creating you should open 50007 port on your modem. How can i create server on all computers without port enabling? Torrent-clients do it somehow. Thanks.

+3  A: 

The question is a little confusing, but I will try to help out. Basically, if the port (50007) is blocked on the server machine by a firewall, you will NOT be able to make a tcp connection to it from the client. That is the purpose of the firewall. A lot of protocols (SIP and bittorrent for example) do use firewall and NAT navigation strategies, but that is a complex subject that you can get more information on here. You will note that to use bittorrent effectively, you have to enable port forwarding for NAT and unblock port ranges for firewalls. Also, bittorrent uses tcp connections for most data transfer. Here is the takeaway:

First, note that there are two types of connections that the BitTorrent program must make:

  • Outbound HTTP connections to the tracker, usually on port 6969.
  • Inbound and outbound connections to the peer machines, usually on port 6881 and up.
Shane C. Mason
>you have to enable port forwarding for NAT and unblock port ranges for firewallsCan i do that using python? Or it's not programming-part?
Ockonal
That would NOT be a programmatic thing, unless the firewall was software based. That is a network sys-admin sort of thing.
Shane C. Mason
+1  A: 

Very difficult to understand your question...

(...) Torrent-clients do it somehow.

The Torrent-clients can do this only when the router -- Internet gateway device (IGD) -- supports the uPNP protocol. The interesting part for your problem is the section about NAT traversal.

jk