Question

In: Computer Science

Write two python codes for a TCP client and a server using a Socket/Server Socket. The...

Write two python codes for a TCP client and a server using a Socket/Server Socket. The client sends a file name to the server. The server checks if there is any integer value in the file content. If there is an integer number, the server will send a message to the client “Integer exists” otherwise, the message “Free of Integers”

Solutions

Expert Solution

We send the file name "test.txt" to the server. The server reads the file, and sends the response appropriately.

Code server.py:

import socket 
   
# take the server name and port name 
host = 'local host'
port = 5000
   
# create a socket at server side 
# using TCP / IP protocol 
s = socket.socket(socket.AF_INET,  
                  socket.SOCK_STREAM) 
   
# bind the socket with server 
# and port number 
s.bind(('', port)) 
   
# allow maximum 1 connection to 
# the socket 
s.listen(1) 
   

# wait till a client accept 
# connection 
c, addr = s.accept() 

filename = c.recv(1024).decode()#receiving the filename from the client


value = "Free of Integers"

f =  open(filename,'r')
while True:
    #read by character
    char = f.read(1)
    if not char:
        break
    if(char.isdigit()):
        value = "Integer exists"
        break


#sending response to client
c.send(value.encode())
# disconnect the server 
c.close() 

Code client.py :

import socket 
   
# take the server name and port name 
   
host = 'local host'
port = 5000
   
# create a socket at client side 
# using TCP / IP protocol 
s = socket.socket(socket.AF_INET, 
                  socket.SOCK_STREAM) 
   
# connect it to server and port  
# number on local computer. 
s.connect(('127.0.0.1', port)) 

filename = "test.txt"#file name to be sent to the server
s.send(filename.encode())

# receive message string from 
# server, at a time 1024 B 
msg = s.recv(1024) 
   
# repeat as long as message 
# string are not empty 
while msg: 
    print(msg.decode()) 
    msg = s.recv(1024) 
  
# disconnect the client 
s.close() 

File:

Output:


Related Solutions

write a skeleton Python TCP servers as follows: Multi-threaded concurrent TCP server using Python’s low-level socket...
write a skeleton Python TCP servers as follows: Multi-threaded concurrent TCP server using Python’s low-level socket module and Python’s threading library Python sockets API: s = socket(), s.bind(), s.listen(), remote_socket, remote_addr = s.accept() Python threading API: thread = threading.Thread(target, args, daemon), thread.start()
Write and test a python program to demonstrate TCP server and client connections. The client sends...
Write and test a python program to demonstrate TCP server and client connections. The client sends a Message containing the 10 float numbers (FloatNumber[]) , and then server replies with a message containing maximum, minimum, and average of these numbers.
Write a Python Client/Server Socket Program that will allow you to send text messages between the...
Write a Python Client/Server Socket Program that will allow you to send text messages between the server and client, terminating when the exit is typed on the client. Build the program on your 2-VM’s and get it working. Cut and paste your code below along with screen shots of your program working.
Write a Java program that establishes a TCP connection with a mail server through the socket...
Write a Java program that establishes a TCP connection with a mail server through the socket interface, and sends an email message. Your program sends SMTP commands to local mail server, receives and processes SMTP commands from local mail server. It sends email message to one recipient at a time. The email message contains a typical message header and a message body. Here is a skeleton of the code you'll need to write: import java.io.*; import java.net.*; public class EmailSender...
How do I make a simple TCP python web client and web server using only "import...
How do I make a simple TCP python web client and web server using only "import socket"? Basically, the client connects to the server, and sends a HTTP GET request for a specific file (like a text file, HTML page, jpeg, png etc), the server checks for the file and sends a copy of the data to the client along with the response headers (like 404 if not found, or 200 if okay etc). The process would be: You first...
4. How does a client socket interact with a server socket to exchange data?
4. How does a client socket interact with a server socket to exchange data?
In Java and using JavaFX, write a client/server application with two parts, a server and a...
In Java and using JavaFX, write a client/server application with two parts, a server and a client. Have the client send the server a request to compute whether a number that the user provided is prime. The server responds with yes or no, then the client displays the answer.
How to create a FTP server using python and TCP Ports How to create a FTP...
How to create a FTP server using python and TCP Ports How to create a FTP server using python and UDP Ports
Suppose a TCP client needs to send 3 packets to the TCP server. Before sending the...
Suppose a TCP client needs to send 3 packets to the TCP server. Before sending the first packet, the estimated RTT is 50 ms, and the estimated deviation of the sample RTT is 10 ms. The parameters α= 0.1, and β = 0.2. The measured sample RTT for the three packets are 60ms, 70 ms, and 40 ms, respectively. Please compute the time out value that was set for each packet right after it is being transmitted out.
Assume a client uses TCP to send data to a server. The TCP header has 12...
Assume a client uses TCP to send data to a server. The TCP header has 12 bytes of options and 99 bytes of data. [Please don't forget Ethernet Type/Len field is 2 bytes in size.] 1. Calculate the total number of bytes passed to the IP layer by the TCP layer. 2. Calculate the total number of bytes passed to the network layer by the IP layer, assuming the IP layer has no options. 3. Calculate the total number of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT