How to open a specific port for python-socket service?

May 23, 2019 595 views
Firewall DigitalOcean Cloud Firewalls Ubuntu 16.04 Python Networking Getting Started DigitalOcean

I’m trying to run a simple python server on my droplet, with the python script below, and it works fine on my friend’s alicloud server. I guess it was the port I used was not configed correctly, but found out the firewall in ubuntu was not enabled. So i added some new rules to the cloud firewall to open the port 8005(which i intend to ues), but it still doesn’t work. Can anyone tell me how to config this?

# coding:utf-8

import socket

from multiprocessing import Process

def handle_client(client_socket):

    request_data = client_socket.recv(1024)
    print("request data:", request_data)

    response_start_line = "HTTP/1.1 200 OK\r\n"
    response_headers = "Server: My server\r\n"
    response_body = "<h1>Python HTTP Test</h1>"
    response = response_start_line + response_headers + "\r\n" + response_body

    client_socket.send(bytes(response, "utf-8"))


if __name__ == "__main__":
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind((name, 8005))

    while True:
        client_socket, client_address = server_socket.accept()
        print("[%s, %s]user connected" % client_address)
        handle_client_process = Process(target=handle_client, args=(client_socket,))

and here’s the errorcode: ConnectionRefusedError: [WinError 10061]
thx a lot!!!

1 comment
Be the first one to answer this question.