Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python +sockets

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.

like image 754
Max Frai Avatar asked Apr 21 '09 18:04

Max Frai


People also ask

What is a socket in Python?

Sockets and the socket API are used to send messages across a network. They provide a form of inter-process communication (IPC).

Is Python good for socket?

Python provides two levels of access to network services. At a low level, you can access the basic socket support in the underlying operating system, which allows you to implement clients and servers for both connection-oriented and connectionless protocols.

How many types of sockets are there in Python?

There are two types of Sockets: the datagram socket and the stream socket.

Does socket use TCP or UDP Python?

The second argument determines the socket type; socket. SOCK_DGRAM is UDP, socket. SOCK_STREAM is a TCP socket. This all provided you are using a AF_INET or AF_INET6 socket family.


2 Answers

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.
like image 105
Shane C. Mason Avatar answered Sep 22 '22 16:09

Shane C. Mason


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.

like image 42
jk. Avatar answered Sep 20 '22 16:09

jk.