xxxxxxxxxx
import socket
# Create a TCP socket
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect to a server using the socket
server_address = ('localhost', 8080) # Replace with the desired server address and port
tcp_socket.connect(server_address)
# Send data to the server
data = b"Hello, server!" # Replace with your desired data
tcp_socket.sendall(data)
# Receive data from the server
response = tcp_socket.recv(1024) # Adjust the buffer size as per your requirements
# Process the received data as needed
print("Received:", response.decode())
# Close the socket connection
tcp_socket.close()
xxxxxxxxxx
#!/usr/bin/python
import socket
import cPickle
import os
import sys
import signal
PORT = 54321
def handle(cs, addr):
print "Conn from", addr
cs.sendall("HAI\n")
try:
l = cPickle.loads(cs.recv(1024))
s = sum(l)
cs.sendall("%d\n" % s)
except:
cs.sendall("fail :(\n")
cs.sendall("bye\n")
cs.close()
signal.signal(signal.SIGCHLD, signal.SIG_IGN)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("0.0.0.0", PORT))
s.listen(100)
while 1:
(cs, addr) = s.accept()
pid = os.fork()
if pid == 0:
s.close()
handle(cs, addr)
sys.exit(0)
cs.close()