On Tue, Oct 24, 2017, 1:32 AM T Obulesu <[email protected]> wrote: > > 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. > >
Looks like what you want is for your ChatServer to be one class that has the responsibility of accepting socket connections, receiving, and sending for all connected clients. And then you want another class that is a ChatClient which allows a connection to the chat server on port 8880 and let's the client to send and receive messages. Does that sound like the goal? Or are you trying to just establish a bi-directional request/reply between two peers? Is this a simplified version of your code or the actual code you are running? There are some issues with this code. It starts binding and listening immediately in the constructor with no way to stop it. I would suggest moving it to a start method and adding a way to stop it. Even once you do that, it blocks the thread while accepting connections. Is that what you want or are you running this chat server in another thread? While it is accepting connections it constantly replaces the last connection with the next one. Is that what you are after? You have set the listen to 5 so it implies you expect multiple clients. But nothing is set up to handle each individual connection. I would expect that you are either going to handle a single connection at a time in that same thread, or to hand off the connection to a thread, or to use a select operation to handle accepting new connections and servicing existing ones asynchronously in the same thread. Can you explain what the role is, for the other two modules that want to use this ChatServer? Are they meant to be clients that can chat to each other? Or is one trying to send data and the other is trying to receive? Can you describe your expected workflow so I can better understand how each of those other modules should actually be using this ChatServer to achieve their goal? > On Saturday, 21 October 2017 16:33:27 UTC+5:30, Justin Israel wrote: > >> >> >> On Sat, Oct 21, 2017, 11:06 PM T Obulesu <[email protected]> wrote: >> >>> Hi All, >>> >>> 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? >>> >> >> >> It might help if you explain a little more concretely about what problem >> you are actually trying to solve. Are you trying to create a generic socket >> connection class that knows how to send and receive, and then use send >> functionality in one subclass, and receive functionality in another? >> >> What code have you written thus far and where are you stuck? At this >> stage it seems a bit code-golfy to try and just write your specification >> for you. >> >> Are you new to python 3 and looking for a newer way to solve this problem >> as opposed to how you would have done it via python 2? Also... Is this >> homework? >> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "Python Programming for Autodesk Maya" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to [email protected]. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/python_inside_maya/d457de3d-4353-47b4-906c-187d616cd9fe%40googlegroups.com >>> <https://groups.google.com/d/msgid/python_inside_maya/d457de3d-4353-47b4-906c-187d616cd9fe%40googlegroups.com?utm_medium=email&utm_source=footer> >>> . >>> For more options, visit https://groups.google.com/d/optout. >>> >> -- > You received this message because you are subscribed to the Google Groups > "Python Programming for Autodesk Maya" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/python_inside_maya/be550507-7ccd-4a99-bd56-cebfda87f24f%40googlegroups.com > <https://groups.google.com/d/msgid/python_inside_maya/be550507-7ccd-4a99-bd56-cebfda87f24f%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA0fMLT%2BTKS9qoA--ZUJc9KwmYa8sHsTBrujmYRRU%2B9u1A%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
