-1

Hello fellow programmers! I'm trying to send a string from one python 3 program on my laptop to another python 3 program on my other laptop.

I think I have searched the whole web for tutorials on using sockets but they don't seem to solve my problem what so ever. I found helpful answers here on stackoverflow, but unfortunately didn't do trick.

How would I send a message like this: "Test message 123" to my other laptop?

I tried this

# Save as server.py
# Message Receiver
import os
from socket import *
host = ""
port = 13000
buf = 1024
addr = (host, port)
UDPSock = socket(AF_INET, SOCK_DGRAM)
UDPSock.bind(addr)
print "Waiting to receive messages..."
while True:
    (data, addr) = UDPSock.recvfrom(buf)
    print "Received message: " + data
    if data == "exit":
        break
UDPSock.close()
os._exit(0)


# Save as client.py
# Message Sender
import os
from socket import *
host = "XXX" # set to IP address of target computer
port = 13000
addr = (host, port)
UDPSock = socket(AF_INET, SOCK_DGRAM)
while True:
    data = input("Enter message to send or type 'exit': ")
    UDPSock.sendto(data, addr)
    if data == "exit":
        break
UDPSock.close()
os._exit(0)

Malthe Have Musaeus
  • 357
  • 1
  • 4
  • 13

1 Answers1

1

https://docs.python.org/2.4/lib/socket-example.html

Start server on your machine 1 Start client on your machine 2

Make sure they are connected to the same network and you can ping one machine from the other.

Be sure you use the port which is open for such connectivity purpose.

Dariusz Krynicki
  • 2,544
  • 1
  • 22
  • 47