Skip to content

Instantly share code, notes, and snippets.

@jamby1100
Last active December 24, 2021 10:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamby1100/faf10bbd148e500f9fb20c4f7274cd24 to your computer and use it in GitHub Desktop.
Save jamby1100/faf10bbd148e500f9fb20c4f7274cd24 to your computer and use it in GitHub Desktop.
This snippet is based off the work of Mathew Wachter (https://github.com/matthewwachter/py-tcp-threaded-server). I added modifications to allow it to communicate to Lambda via requests
import json
import socket
import os
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect the socket to the port where the server is listening
ip_address = os.getenv("TCP_SERVER_IP_ADDRESS")
port = int(os.getenv("TCP_PORT_NUMBER"))
server_address = (ip_address, port)
sock.connect(server_address)
msg = 'this is the request'
# msg = json.dumps(data)
# Send the message
sock.sendall(msg.encode('utf-8'))
# Receive the message back
res = sock.recv(1024).decode('utf-8')
print("AND THE RESPONSE IS")
print(res)
data = json.loads(res)
print(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment