Skip to content

Instantly share code, notes, and snippets.

@vanjikumaran
Created October 2, 2017 22:52
Show Gist options
  • Save vanjikumaran/137f6041cb813a14f057e6a89a71b476 to your computer and use it in GitHub Desktop.
Save vanjikumaran/137f6041cb813a14f057e6a89a71b476 to your computer and use it in GitHub Desktop.
very simple TCP server made out of Python | To run $ python MockServer.py
# Copyright 2017 Vanji
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import socket
import sys
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the address given on the command line
server_address = ('localhost', 4444)
sock.bind(server_address)
print >>sys.stderr, 'starting up on %s port %s' % sock.getsockname()
sock.listen(10)
while True:
print >>sys.stderr, 'waiting for a connection'
connection, client_address = sock.accept()
try:
print >>sys.stderr, 'client connected:', client_address
data = 'My TCP example'
print >>sys.stderr, 'received "%s"' % data
if data:
connection.sendall(data)
else:
break
finally:
connection.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment