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.
Sockets and the socket API are used to send messages across a network. They provide a form of inter-process communication (IPC).
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.
There are two types of Sockets: the datagram socket and the stream socket.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With