0

I want to create a mediator python program, which will receive data from a socket and transfer to other specified socket and vice versa. here is code, but its not functioning. Any Suggestion will be appreciated.

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()
  • 2
    What does "not functioning" mean? What are you expecting to happen, and what's happening instead? Be specific. – Wooble Jan 30 '14 at 12:58
  • The type of program you want to build is also called 'proxy'. – User Jan 30 '14 at 13:36
  • @Wooble , its connecting to one socket and get data from it but not passing to another socket. – Virendra Ashiwal Jan 30 '14 at 14:16
  • @User : could you please specifiy it more, sorry i m begginer of python. or some link will be helpful – Virendra Ashiwal Jan 30 '14 at 14:18
  • @User : Please will you elobrate more what is 'proxy' meaning here or any link ? – Virendra Ashiwal Feb 03 '14 at 11:03
  • @Wooble : Expected result is, Mediator Python program should listen any socket, and then compare it to pre exist IP adress (as per mention in code like HOST_1 , or HOST_2 or HOST_3) by using 'if elif function' and if the 'if condition' is true then, it should send recieved data from that socket and sent it to another sócket(example: if it get connection from HOST_1 , then it will re check the ip address by using 'if elif' function and then send the data from HOST_1 to HOST_3). – Virendra Ashiwal Feb 03 '14 at 11:12
  • Hello, for me your program looks like a proxy: http://en.wikipedia.org/wiki/Proxy_server Have a look at the examples and references if it matches. I wanted to introduce this term because I thought that you could have also a vocabulary problem - a language barrier that exists also between sciences. – User Feb 03 '14 at 13:34
  • Thank you for responce. in between i search about this and understood that, Proxy server and Port forward both are same. here i got one link, http://www.voorloopnul.com/blog/a-python-proxy-in-less-than-100-lines-of-code/ – Virendra Ashiwal Feb 03 '14 at 14:17

0 Answers0