Python程序,作为两个套接字之间的中介

时间:2014-01-30 12:04:48

标签: sockets python-3.x network-programming serversocket

我想创建一个中介python程序,它将从套接字接收数据并传输到其他指定的套接字,反之亦然。 这是代码,但它不起作用。 任何建议将不胜感激。

import socket
import time
import math
import json
import datetime

#HOST = socket.gethostname()                 
HOST = ''                 # Symbolic name meaning all available interfaces
HOST_1 = '131.246.75.72'  # My PC
HOST_2 = '131.246.36.61'  # My Laptop
HOST_3 = '131.246.75.72'  # Ramin PC
HOST_4 = '131.246.119.65'  # Remote Server

PORT = 12345 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print('Hello Connected by', addr)

if addr  == HOST_1 :
    print('Host is My PC')
    while True:
        data = conn.recv(1024)
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((HOST_3, PORT))
        s.sendall(json.dumps(data))
        s.close()
elif addr  == HOST_3 :
    while True:
        data = conn.recv(1024)
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((HOST_1, PORT))
        s.sendall(json.dumps(data))
        s.close()

0 个答案:

没有答案