簡體   English   中英

如何從一個套接字接收數據並將其發送到python中的另一個套接字?

[英]How to recieve data from a one socket and send it to another socket in python?

我是Python和編程的新手。 所以,如果我要問的是愚蠢的,請原諒我。 所以我有這個應用程序,它通過服務器的端口5555連接到服務器。 因此,我將應用程序重新配置為連接到本地主機的5555端口,而不是實際的服務器。 然后,我編寫了一個python程序來偵聽127.0.0.1:5555並將所有數據發送到實際服務器。 這是程序

import socket

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind(("localhost", 5555))
    s.listen()
    conn, addr = s.accept()
    with conn:
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s2:
            s2.connect(("actualserver",5555))
            while True:
                data = conn.recv(4096)
                if data:
                    print('Sent to the Server :', data)
                    s2.sendall(data)
                data1 = s2.recv(4096)
                if data1:
                    print('Received from the Server :', data1)
                    conn.sendall(data1)

因此,問題是應用程序嘗試連接到服務器,並給我錯誤消息“服務器未響應!”。 這個程序給出這樣的輸出

Sent to the Server : /x87 /x65 blah blah blah
Sent to the Server : /x87 /x65 blah blah blah
Sent to the Server : /x87 /x65 blah blah blah
And an error, saying connection disconnected by an application of your host

我不明白問題是什么。 請幫忙。

更改已打開5555端口的127.0.0.1或另一個IP上的localhostactualserver

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM