#!/usr/bin/env python
"""
An annotated simple socket client example in python.
WARNING: This example doesn't show a very important aspect of
TCP - TCP doesn't preserve message boundaries. Please refer
to http://blog.stephencleary.com/2009/04/message-framing.html
before adapting this code to your application.
Runs in both python2 and python3.
"""
import socket
# Note that the server may listen on a specific address or any address
# (signified by the empty string), but the client must specify an address to
# connect to. Here, we're connecting to the server on the same machine
# (127.0.0.1 is the "loopback" address).
SERVER_ADDRESS = '127.0.0.1'
SERVER_PORT = 22222
# Create the socket
c = socket.socket()
# Connect to the server. A port for the client is automatically allocated
# and bound by the operating system
c.connect((SERVER_ADDRESS, SERVER_PORT))
# Compatibility hack. In python3, input receives data from standard input. In
# python2, raw_input does exactly that, whereas input receives data, then
# "evaluates" the result; we don't want to do that. So on python2, overwrite
# the input symbol with a reference to raw_input. On python3, trap the
# exception and do nothing.
try:
input = raw_input
except NameError:
pass
print("Connected to " + str((SERVER_ADDRESS, SERVER_PORT)))
while True:
try:
data = input("Enter some data: ")
except EOFError:
print("\nOkay. Leaving. Bye")
break
if not data:
print("Can't send empty string!")
print("Ctrl-D [or Ctrl-Z on Windows] to exit")
continue
# Convert string to bytes. (No-op for python2)
data = data.encode()
# Send data to server
c.send(data)
# Receive response from server
data = c.recv(2048)
if not data:
print("Server abended. Exiting")
break
# Convert back to string for python3
data = data.decode()
print("Got this string from server:")
print(data + '\n')
c.close()
click below button to copy the code. By Socket tutorial team
socket programming in c example client serverc socket client exampletcp client server program in c linuxtcp client server chat program in cclient server socket programming in c linuxtcp echo client server program in chow to run client server program in linuxmultiple client server program in cwebsocket endpoint examplewebsocket endpoint from client sidea message sent on a websocket from a client side can be synchronous as well as asynchronous.javax.websocket.server.serverendpoint jar@onopengroup of websocket annotation endpoints@clientendpoint examplewhich of the following will call a websocket endpoint from the client side?socket socket io python socket java socket socks proxy socket programming socket programming in java network programming node js socket io java serversocket network socket socket programming in c socket c what is a socket tcp socket java networking python socket programming nodejs socket socket types socket connect linux socket python network programming java network programming serversocket socket internet socket error unix socket unix network programming socket io tutorial sockets tutorial java socket tutorial python socket tutorial java networking tutorial socket programming tutorial socket programming in c tutorial java socket programming tutorial c socket tutorial android socket tutorial network programming tutorial php socket tutorial python socket programming tutorial python networking tutorial linux socket tutorial