2

I have begun learning Socket Programming and Issue I faced is. I am unable to connect Sockets when On two different network ( To be specific : I am using Web-host and Cgi programming to create python socket Server and my goal is to connect to that socket using desktop client python application )

My Server Coad : Location Public_html/cgi-bin/serverSocket.py

#!/usr/bin/python
print "Content-type: text/html\n\n";

import cgitb
import socket


cgitb.enable()

def main():
    host = 'localhost'
    port = 8111

    s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        s.bind((host,port))
    except socket.error as e:
        print(str(e))

    s.listen(10)
    c,addr = s.accept()
    print("Connection From : " + str(addr))
    while True:
        data = c.recv(1024)
        if not data:
            break
        print ("From Connected user : " + str(data.decode()))
        data =str(data.decode()).upper()
        print ("sending :" + str(data))

        c.send(data.encode())

if __name__ == '__main__':
    main()

And Client Program : Location On My Local Computer C:/Desktop

#!/usr/bin/python
print "Content-type: text/html\n\n";


#Client Socket Program

import socket

def main():
    host = 'www.mywebsite.com'
    port = 8111

    s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)    
    try:
        s.connect((host,port))
    except socket.error as e:
        print(str(e))

    message=input("-> ")
    while message != 'q':

        s.send(message.encode())
        print("Sent Message")
        data=s.recv(1024)
        print('Recieved from server :',  str (data.decode()))
        message=input("->")
    s.close()


if __name__ == '__main__':
    main()

| Error Encountered is : [WinError 10060] | | Python Server uses : Python 2.6.6 | | Python Client :python 3.4 |

While using This On same system (ie: Local host as Server and client works Fine)

PS: Also link if there is any tutorial on this,Also advice some configuration if must be made.

amitnair92
  • 487
  • 7
  • 20
  • Did you try connecting from your local computer using another tool, such as `netcat`? It might just be a firewall blocking the incoming connection. – spectras Jun 09 '16 at 04:39

2 Answers2

2

If you want them to access each other, use public IPs. (Public IP Address) You would also need to port-forward (this is different for every router so I can't explain, look it up). Otherwise, the port you want to access will not be accessible from other networks. When you port-forward, that port on your Public IP Address can then be accessed.

anonymous
  • 271
  • 3
  • 16
  • What if someone uses a TCP tunneling like e.g. ngrok? Does one need port-forwarding in that case too? – topkek Jan 05 '21 at 14:43
0

Python 2 and 3 handle sockets differently! See here this question for example. What you could do as a quick workaround is to change your client start line to "#!/usr/bin/env python2.7" to force the client to use python 2 as well, that should fix your problem.

Community
  • 1
  • 1
VBB
  • 1,305
  • 7
  • 17
  • Err, no they don't. The question you linked shows that text encoding is handled differently (which may have an impact if you try to send text). But it's unrelated to his “Connection timeout” error, which happens before anything can be sent anyway. – spectras Jun 09 '16 at 04:46
  • Ah okay! Are you sure your network settings allow you to communicate between these two machines? For instance, can you programatically connect to say google from your client computer? Does your server respond to pings on that port? – VBB Jun 09 '16 at 04:49
  • I have turned Windows Firewall of and tried again. Does not work this time either. are there any configuration on server Side to be made? I am using SSH to run ServerSocket.py on server. – amitnair92 Jun 09 '16 at 05:39