Sockets but calling from different programs

T Obulesu obulesu.t at gmail.com
Mon Oct 23 08:33:40 EDT 2017


I'm new to python3 and scratching my head to write a program for this logic:

classA.py
Class A:
          # class for socket communication
          basic init method that initializes port, address, connection

         method send(message):
         # for sending any message through the given port


       method receive():
       # will be keep on listening for incoming messages



classB.py

Class B:
           import the send method from class A
           send the messages from this class


classC.py

Class C:
            import the receive method from the class A
            receive all the messages from the same socket from here.



Note:
 classA.py, classB.py, ClassC.py are saved in different locations.


Can someone help me in writing the code and how can I create a single object and use it in multiple classed?

This is how my code looks like:
import socket
import select

class ChatServer:

    def __init__( self):
        self.port = 8880;
        self.srvsock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
        self.srvsock.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 )
        self.srvsock.bind( ("", 8880) )
        self.srvsock.listen( 5 )
        print("New socket bind")
        while 1:
            print("Waiting for new connection")
            self.sock,(remhost,remport)=self.accept_new_connection()
        print ('ChatServer started on port %s' % port)

    def send(self,data):
        self.sock.send(data)
    def receieve(self):
        self.str=sock.recv(1024)
        print(self.str)


I want to run this code first time when my project starts and want to call send() and receive() functions from other two different python files located in different path.
  



More information about the Python-list mailing list