In Python I am trying to run a server and a client seperately and get them to co
ID: 3700915 • Letter: I
Question
In Python I am trying to run a server and a client seperately and get them to communicate. I have the server running in Terminal on a mac and I am running the client on a seperate instance of terminal. I can't figure out how to get them to communicate. I need to know the correct host address, port number and what I am doing wrong.
#Server
#!/usr/bin/python
import socket
import sys
def main():
my_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
if len(sys.argv) == 2:
port = sys.argv[1]
else:
port = input("Enter port number:")
server_address = ('localhost', int(port))
print(sys.stderr, 'starting up on %s port %s' % server_address)
my_socket.bind(server_address)
message_count = 0
message = ["Mongo only pawn in the game of life --Mongo",
"Your mother was a hamster and your father smelt of elderberries! -The Holy Grail French",
"A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools. - The Hitchhiker's Guide to the Galaxy",
"No matter where you go, there you are - Buckaroo Banzai"]
while True:
print (sys.stderr, '/nwaiting to receive message')
data, address = my_socket.recvfrom(4096)
print(sys.stderr, 'received %s bytes from %s' % (len(data), address))
print(sys.stderr, data)
if data:
sent = my_socket.sendto(message[message_count].encode(), address)
message_count += 1
if message_count > len(message) - 1:
message_count = 0
print(sys.stderr, 'sent %s bytes back to %s' % (sent, address))
if __name__== "__main__":
main()
#Client
#!/usr/bin/python
import socket
import sys
def main():
my_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
if len(sys.argv) == 3:
host = sys.argv[1]
port = sys.argv[2]
else:
host = input("Enter the host address:")
port = input("Enter port number:")
if host == 'localhost':
host = '127.0.0.1'
dest_address = (host, int(port))
count = 0
rcvdQOTD = False
while count < 3:
try:
print(sys.stderr, 'Sending ')
my_socket.settimeout(10)
sent = my_socket.sendto(' '.encode(), dest_address)
print(sys.stderr, 'Waiting to receive')
data, server = my_socket.recvfrom(4096)
my_socket.settimeout(None)
if host in server:
print(sys.stderr, 'received "%s"' % data)
rcvdQOTD = True
count = 3
else:
print('Wrong server')
except socket.timeout:
print ("Timeout from server")
count += 1
my_socket.close()
if rcvdQOTD == False:
print("Tried to connect once and retried twice. Connection timed out")
finished = input("Finished?")
if __name__== "__main__":
main()
Explanation / Answer
serever.py: include these steps at begning of your server-side
import socket # Import socket module
port = 60000 # Reserve a port for your service.
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
s.bind((host, port)) # Bind to the port
s.listen(5)
-------------------------------------------------------------------------------------
client.py: include these steps at begning of your client-side
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 60000 # Reserve a port for your service.
s.connect((host, port))
s.send("Hello server!")
Note : if Port no. should be used same e.g. 60000 at both-side.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.