Python cannot go over internet network

Posted by user1642826 on Super User See other posts from Super User or by user1642826
Published on 2012-09-26T00:36:02Z Indexed on 2012/10/04 9:41 UTC
Read the original article Hit count: 182

Filed under:
|
|

I am currently trying to work with python networking and I have reached a bit of a road block. I am not able to network with any computer but localhost, which is kind-of useless with what networking is concerned. I have tried on my local network, from one computer to another, and I have tried over the internet, both fail. The only time I can make it work is if (when running on the server's computer) it's ip is set as 'localhost' or '192.168.2.129' (computers ip).

I have spent hours going over opening ports with my isp and have gotten nowhere, so I decided to try this forum. I have my windows firewall down and I have included some pictures of important screen shots. I have no idea what the problem is and this has spanned almost a year of calls to my isp. The computer, modem, and router have all been replaced in that time.

Screen shots:

import socket
import threading
import socketserver

class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):

    def handle(self):
        data = self.request.recv(1024)
        cur_thread = threading.current_thread()
        response = "{}: {}".format(cur_thread.name, data)
        self.request.sendall(b'worked') 

class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
    pass

def client(ip, port, message):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((ip, port))
    try:
        sock.sendall(message)
        response = sock.recv(1024)
        print("Received: {}".format(response))
    finally:
        sock.close()

if __name__ == "__main__":
    # Port 0 means to select an arbitrary unused port
    HOST, PORT = "192.168.2.129", 9000

    server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)
    ip, port = server.server_address

    # Start a thread with the server -- that thread will then start one
    # more thread for each request
    server_thread = threading.Thread(target=server.serve_forever)
    # Exit the server thread when the main thread terminates
    server_thread.daemon = True
    server_thread.start()
    print("Server loop running in thread:", server_thread.name)
    ip = '12.34.56.789'
    print(ip, port)

    client(ip, port, b'Hello World 1')
    client(ip, port, b'Hello World 2')
    client(ip, port, b'Hello World 3')

    server.shutdown()

I do not know where the error is occurring. I get this error:

Traceback (most recent call last):
  File "C:\Users\Dr.Frev\Desktop\serverTest.py", line 43, in <module>
    client(ip, port, b'Hello World 1')
  File "C:\Users\Dr.Frev\Desktop\serverTest.py", line 18, in client
    sock.connect((ip, port))
socket.error: [Errno 10061] No connection could be made because the target machine 
actively refused it 

Any help will be greatly appreciated.

*if this isn't a proper forum for this, could someone direct me to a more appropriate one.

© Super User or respective owner

Related posts about python

Related posts about networking