Python Socket is not receiving messages sent to it

Question:

I made a socket connection between a client and server. I set it up so it makes a request for data, but it doesn’t receive the data. It throws Traceback (most recent call last): File "C:...file path...server.py", line 38, in <module> s1.connect((host1, port1)) ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it, but it sends a response. How can I set it up to receive the message? By the way, it makes a request to the server to read a file.

Server.py:

import json
import socket
import base64
while True:
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.bind((HOST, PORT))
        s.listen()
        conn, addr = s.accept()
        with conn:
            print('Connected by', addr)
            while True:
                data = conn.recv(1024)
                data = repr(data)
                data = str(data)
                data1 = []
                for i in range(len(data)):
                    data1.append(data[i])
                data1[0] = ""
                data1[1] = ""
                data1[len(data1)-1] = ""
                data ="".join(data1).replace("'",""").replace("~","=")
                if (data != ""):
                    print(data)
                    data = json.loads(data)
                    typer = data["type"]
                    if (typer == 'putreq'):
                        #Writes to file, there are no bugs here.
                    else:
                        host1 = addr[0]
                        port1 = addr[1]
                        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s1:
                            s1.connect((host1, port1))
                            with open(data["name"], 'r') as userfile:
                                data1 = userfile.read()
                            s1.sendall(bytes(base64.b64encode(bytes(data1,'utf-8')),'utf-8'))
                            s1.close
                    s.close()

Client.py:

import socket
import sys
import base64
import json
import random
import time

typec = sys.argv[1]
filec = sys.argv[2]
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.connect((HOST, PORT))
        s.sendall(bytes(str({"type":'namereq',"name":filec}), "UTF-8"))
        data = s.recv(1024)
        data = repr(data)
        data = str(data)
        data1 = []
        for i in range(len(data)):
            data1.append(data[i])
        data1[0] = ""
        data1[1] = ""
        data1[len(data1)-1] = ""
        data ="".join(data1).replace("~","=")
        if(data != ''):
            print(data)

I think it has to do with the hostname and port being different on the server and the user.

Asked By: Zenzicubic

||

Answers:

modify this:

                    else:
                        host1 = addr[0]
                        port1 = addr[1]
                        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s1:
                            s1.connect((host1, port1))
                            with open(data["name"], 'r') as userfile:
                                data1 = userfile.read()
                            s1.sendall(bytes(base64.b64encode(bytes(data1,'utf-8')),'utf-8'))
                            s1.close

into this:

                else:
                    with open(data["name"], 'r') as userfile:
                        data1 = userfile.read()
                    conn.sendall(bytes(base64.b64encode(bytes(data1,'utf-8')),'utf-8'))
                    conn.close

you already have a socket connected to that host and port no need to create others (also because i can see that HOST is equal to host1)

Answered By: gekigek99
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.