Connecting a socket over the Internet in Python?

I created a basic socket program for client servers in Python 2.7.x, and it works absolutely fine on the same network even on different computers, but when I start the server and client on different networks (the server on my network is friends, and the client is mine) it returns no error and continues to wait. I just don’t understand how to debug the code. I use port 80, killing all services on port 80. I also did port forwarding on port 80 on both machines.

My codes are as follows:

client.py

import socket              

s = socket.socket()        
host = '103.47.59.130' 
port = 80               

s.connect((host, port))
while True: 
    print "From Server: ", s.recv(1024)  #This gets printed after sometime
    s.send(raw_input("Client please type: "))

s.close()                     

server.py

import socket               

s = socket.socket()         # Create a socket object
host = '192.168.0.104'    #private ip address of machine running fedora
port = 80                
s.bind((host, port))       

s.listen(5)                
c, addr = s.accept()       
print 'Got connection from', addr    #this line never gets printed
while True:
   c.send(raw_input("Server please type: "))
   print "From Client: ", c.recv(1024)

c.close()                

Sometimes it outputs ** From the server: **, but does not send any messages back and forth.

PS: I used to look at Stack Overflow, but I can’t find anything relevant.

+4
3

. , 5006, , , 80. , :

  • "", , , .
  • (), TCP
  • , 5001 ( )
  • , , 5006 ( )
  • , 5001 , IP-, , 5006 .

, IP- , , 5001. , , , 5006 , - .

:

import socket              

s = socket.socket()        
host = '103.47.59.130' 
port = 5001               

s.connect((host, port))
while True: 
    try:
        print "From Server: ", s.recv(1024) 
        s.send(raw_input("Client please type: "))
    except:
        break
s.close()

:

import socket               

s = socket.socket()         # Create a socket object
host = '192.168.0.104'    #private ip address of machine running fedora
port = 5006                
s.bind((host, port))       

s.listen(5)                
c, addr = s.accept()       
print 'Got connection from', addr    
while True:
   c.send(raw_input("Server please type: "))
   print "From Client: ", c.recv(1024)

c.close()
+2

IP- 0.0.0.0 , / ( ).

, !

+1

, . , , . : , .

, , ping, tracepath, tcpdump nc (netcat).

, netcat, , , .

0
source

Source: https://habr.com/ru/post/1628661/


All Articles