I am trying to send the image from a windows port and receive it in ubuntu using python -jupyter notebook and socket programming. Both Windows and Ubuntu are partitioned in the same laptop.
I am facing issues in identifying the right port and getting the image.
Tried both client.py method and clientserver.py methods but am continuously stuck with errors.
import socket # Import socket module
s = socket.socket() # Create a socket object
host_ip = '10.1.2.107'
port = 47136 ###21
print ('ip address', host_ip)
s.bind((host_ip, port)) # Bind to the port
s.listen(5) # Now wait for client connection.
print ('Server listening....')
while True:
conn, addr = s.accept()
print ('Got connection from', addr)
conn.sendall(b"Hello server!")
data = conn.recv(1024)
print('Server received', repr(data))
filename='/home/dev/Downloads/dev.jpg'
f = open(filename,'r')
l = f.read(1024)
while (l):
conn.send(l)
print('Sent ',repr(l))
l = f.read(1024)
f.close()
print('Done sending')
conn.send('Thank you for connecting')
conn.close()
# client.py
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
host_ip = socket.gethostbyname(host)
port = 631 # Reserve a port for your service.
s.connect((host, port))
s.send("Hello server!")
with open('received_file', 'w') as f:
print ('file opened')
while True:
print('receiving data...')
data = s.recv(1024)
print('data=%s', (data))
if not data:
break
# write data to a file
f.write(data)
f.close()
print('Successfully get the file')
s.close()
print('connection closed')
###============================USING UDP/IP Protocol
import socket
import time
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
host_ip = '10.1.2.207'
port = 5000
data = "my first socket program"
#s.connect((host_ip,port))
s.sendto(b'data', (host_ip,port))
print ("send " + data)
time.sleep(1)
while True:
data_recv , addr = s.recvfrom(1024)
print('Received', data_recv)
s.close()