简体   繁体   English

Python Socket 没有收到发送给它的消息

[英]Python Socket is not receiving messages sent to it

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:\\Users\\jenga\\Desktop\\Jacobs_Python\\net\\server\\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.它抛出Traceback (most recent call last): File "C:\\Users\\jenga\\Desktop\\Jacobs_Python\\net\\server\\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 ,但它发送了响应。 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:服务器.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:客户端.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.我认为这与服务器和用户上的主机名和端口不同有关。

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)您已经有一个连接到该主机和端口的套接字,无需创建其他端口(也是因为我可以看到 HOST 等于 host1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM