1

I have two scripts I am actively using for a programming class. My code is commented nicely and my teacher prefers outside resources since there are many different solutions.

Getting to the actual problem though, I need to create a server with a socket (which works) and then allow another computer to connect to it using a separate script (which also works). The problem is after the connection is made. I want the two to be able to send messages back and forth. The way it sends has to be in byte form with how I have it set up but the byte returned is impossible to read. I can decode it but I want it to be conveniently located in the Command Prompt with everything else. I attempt to import the main script (Connection.py) into the secondary script (Client.py) but then it runs the main script. Is there any way I can prevent it from running?

Here is my main script (the one creating the server)

#Import socket and base64#
import socket
import base64

#Creating variable for continuous activity#
neverland = True

#Create socket object#
s = socket.socket()
print ("Socket created") #Just for debugging purposes#

#Choose port number for connection#
port = 29759 #Used a random number generator to get this port#

#Bind to the port#
s.bind((' ', port))
print ("Currently using port #%s" %(port)) #Just for debugging purposes#

#Make socket listen for connections#
s.listen(5)
print ("Currently waiting on a connection...") #Just for debugging purposes#

#Loop for establishing a connection and sending a message#
while neverland == True:

    #Establish a connection#
    c, addr = s.accept()
    print ("Got a connection from ", addr) #Just for debugging purposes#

    #Sending custom messages to the client (as a byte)#
    usermessage = input("Enter your message here: ")
    usermessage = base64.b64encode(bytes(usermessage, "utf-8"))
    c.send(usermessage)

    #End the connection#
    c.close()

And here is my secondary script (the one that connects to the main one)

#Import socket module#
import socket
import Connection

#Create a socket object#
s = socket.socket()         

#Define the port on which you want to connect#
port = 29759               

#Connect to the server on local computer#
s.connect(('127.0.0.1', port))

#Receive data from the server#
print (s.recv(1024))
usermessage = base64.b64decode(str(usermessage, "utf-8"))
print (usermessage)

#Close the connection#
s.close()  

Upon running them both in the command prompt, the following error occurs:

enter image description here

It attempts to run the main script again and gets the error, how can I prevent it?

Mohideen bin Mohammed
  • 18,813
  • 10
  • 112
  • 118
  • Possible duplicate of [Python Server Client program error: "OSError: \[WinError 10048\]"](https://stackoverflow.com/questions/23596009/python-server-client-program-error-oserror-winerror-10048) – Caders117 May 22 '18 at 05:57

1 Answers1

0

The way you'd commonly achieve this is to not execute any actions when a script is read. I.e. you just define your functions, classes and variables and if this script is meant to be called directly, you if it was called as such and refer to appropriate entry point. E.g.:

myvar = "world"
def main():
    print("Hello", myvar)
if __name__ == "__main__":
    main()

This way you can call your script python example.py or import it from another one and use it content where needed: import example; print(example.myvar).

You can also, and this is not mutually exclusive with above, refactor your scripts and have one file with common/shared definitions which is imported into and used by both of your scripts.

Ondrej K.
  • 8,841
  • 11
  • 24
  • 39