Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
suyashi29
GitHub Repository: suyashi29/python-su
Path: blob/master/Python core/ tcpclient.py.ipynb
3074 views
Kernel: Python 3
import socket import sys # Create a TCP/IP socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Define host host = 'localhost' # define the communication port port = 8080 # Bind the socket to the port sock.bind((host, port)) # Listen for incoming connections sock.listen(1) # Wait for a connection print( 'waiting for a connection') connection, client = sock.accept() print (client, 'connected') # Receive the data in small chunks and retransmit it data = connection.recv(16) print ('received "%s"' % data) if data: connection.sendall(data) else: print ('no data from', client) # Close the connection connection.close()
import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) host ="localhost" port =8000 s.connect((host,port)) def ts(str): s.send('f'.encode()) data = '' data = s.recv(1024).decode() print (data) while 2: r = input('enter') ts(s) s.close ()