Question: import socket import sys def get _ socket _ timeout ( ) : timeout = socket.getdefaulttimeout ( ) if timeout is not None: print (
import socket
import sys
def getsockettimeout:
timeout socket.getdefaulttimeout
if timeout is not None:
printfCurrent default socket timeout is timeout milliseconds."
else:
printNo default socket timeout is set."
def setsockettimeouttimeout:
socket.setdefaulttimeouttimeout
printfDefault socket timeout set to timeout milliseconds."
def performsocketoperationshost port, datatosend:
clientsocket socket.socketsocketAFINET, socket.SOCKSTREAM
try:
printSocket created successfully."
clientsocket.connecthost port
printfConnected to host:port
clientsocket.sendalldatatosend.encode
printfData sent: datatosend
receiveddata clientsocket.recvdecode
printfReceived data: receiveddata
except socket.error as e:
printfSocket error: e
sysexit
finally:
try:
clientsocket.close
printSocket closed."
except socket.error as closeerror:
printfError while closing the socket: closeerror
sysexit
hosttoconnect "python.org"
porttoconnect
messagetosend "Hello kakorhaphiphiostooxiouss!!"
datatosend fPOST HTTPr
Host: hosttoconnectr
ContentLength: lenmessagetosendr
r
messagetosend
try:
performsocketoperationshosttoconnect, porttoconnect, datatosend
except Exception as e:
printfAn error occurred: e
try:
getsockettimeout
setsockettimeout
except Exception as e:
printfAn error occurred: e
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
