Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Sockets but calling from different programs

27 views
Skip to first unread message

T Obulesu

unread,
Oct 23, 2017, 8:33:54 AM10/23/17
to
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.

Lawrence D’Oliveiro

unread,
Oct 24, 2017, 1:42:26 AM10/24/17
to
On Tuesday, October 24, 2017 at 1:33:54 AM UTC+13, T Obulesu wrote:
> 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.

Would something from here <https://github.com/ldo/glibcoro_examples> be useful? In particular, the “hailstone_server” and “hailstone_client” example scripts.

Cameron Simpson

unread,
Oct 25, 2017, 4:38:53 AM10/25/17
to
On 23Oct2017 05:33, T Obulesu <obul...@gmail.com> wrote:
>I'm new to python3 and scratching my head to write a program for this logic:

The tutor list might be a better place for such questions, but since we're
here...

>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?

That is a pretty normal arrangement. Class A might look like this:

class A:
def __init__(self, port, address):
self.connection = ... make your connection to (address, port)
def send(self, msg):
send msg using self.connection ...

Since classes B and C seem expected to share tha same connection, the natural
thing is to set up the connection _separately_ from setting up B and C, and
pass the established connection to each.

So class B might commence:

class B:
def __init__(self, conn, ...):
self.conn = conn
... whatever other initialisation ...
def speak(self, something):
self.conn.send(something)

You'll notice here that we're _not_ importing anything about class A here.
Class B does not need to know class A's name to use it.

Because Python uses duck typing, you could pass _any_ object which has a
.send() method as "conn" to the class B setup. This allows you to write some
other class A2 with those same methods, but using some different type of
connection, and pass that in to classes B and C.

So a main programme might set things up like this:

from classA import A
from classB import B
from classC import C

conn = A(address, port)
sender = B(conn, other-stuff...)
receiver = C(conn, other-stuff...)
B.speak("foo")

and so forth.

Cheers,
Cameron Simpson <c...@cskk.id.au> (formerly c...@zip.com.au)
0 new messages