Communication processing by Python This article is the 21st day article of Keio University SFC Murai & Tokuda Lab Advent Calendar 2015.
Until the summer, I started to touch the PC from the fall semester because I was disgusted with myself talking about research significance and services without implementing it at the level of "what is UNIX command". Then I started to touch Swift and Python and now I'm writing Python.
After all, I'm new to programming. This time I will write about socket communication in Python.
Set up a TCP server using only Python and perform socket communication with a TCP client.
Since the handling of data types is different between python2 series and 3 series, the correction of that is added. (Also print () and input ())
Basically, we will follow the steps using the socket library. First, let's create a TCP client.
client.py (Python2 series)
# -*- coding:utf-8 -*-
import socket
host = "xxx.xxx.xxx.xxx" #Enter the host name of your server
port = xxxx #I will specify an appropriate PORT
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #Create an object
client.connect((host, port)) #Now connect to the server
client.send("from nadechin") #Send the appropriate data (as the recipient knows)
response = client.recv(4096) #Receive should be an appropriate power of 2 (not too large)
print response
client.py (Python3 series)
# -*- coding:utf-8 -*-
import socket
host = "xxx.xxx.xxx.xxx" #Enter the host name of your server
port = xxxx #I will specify an appropriate PORT
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #Create an object
client.connect((host, port)) #Now connect to the server
massage = "from nadechin"
client.send(massage.encode('utf-8')) #Send the appropriate data (as the recipient knows)
response = client.recv(4096) #Receive should be an appropriate power of 2 (not too large)
print(response)
It is the basis of servers and clients that use Python, and it can be widely applied by expanding it. UDP clients can be created in much the same way. (There is no need to call connect () for UDP communication)
Next, let's create a TCP server. Naturally, this is also written in the same way as the TCP client form above.
server.py (Python2 series)
# -*- coding:utf-8 -*-
import socket
host = "xxx.xxx.xxx.xxx" #Enter the host name of your server
port = xxxx #I will specify the same PORT set on the client
serversock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
serversock.bind((host,port)) #Bind by specifying IP and PORT
serversock.listen(10) #Listen for a connection (specify the maximum number of queues)
print 'Waiting for connections...'
clientsock, client_address = serversock.accept() #Store data when connected
while True:
rcvmsg = clientsock.recv(1024)
print 'Received -> %s' % (rcvmsg)
if rcvmsg == '':
break
print 'Type message...'
s_msg = raw_input()
if s_msg == '':
break
print 'Wait...'
clientsock.sendall(s_msg) #Returns a message
clientsock.close()
server.py (Python3 series)
# -*- coding:utf-8 -*-
import socket
host = "xxx.xxx.xxx.xxx" #Enter the host name of your server
port = xxxx #I will specify the same PORT set on the client
serversock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
serversock.bind((host,port)) #Bind by specifying IP and PORT
serversock.listen(10) #Listen for a connection (specify the maximum number of queues)
print('Waiting for connections...')
clientsock, client_address = serversock.accept() #Store data when connected
while True:
rcvmsg = clientsock.recv(1024)
print('Received -> %s' % (rcvmsg))
if rcvmsg == '':
break
print('Type message...')
s_msg = input().replace('b', '').encode('utf-8')
if s_msg == '':
break
print('Wait...')
clientsock.sendall(s_msg) #Returns a message
clientsock.close()
1
Server side (CentOS)
$ python server.py
Waiting for connections...
2
Client side (MacBook)
$ python client.py
3
Server side (CentOS)
$ python server.py
Waiting for connections...
Received -> from nadechin
Type message...
Hello World!
4
Client side (MacBook)
$ python client.py
Hello World!
5
Server side (CentOS)
$ python server.py
Waiting for connections...
Received -> from nadechin
Type message...
Hello World!
Wait...
Received ->
You can have a little chat like this. If you change the method, you can exchange infinitely, or if you ignore the acceptance of the message, you can keep the right to speak even after speaking.
It was an announcement that feels like a small fish, but thank you for giving me the opportunity to write in such a place. In the future, I would like to learn C language little by little and deepen my understanding of networks.
I did socket programming in C language and confirmed that I could actually do it. Recently, I've been eating a lot of things, but I'm starting to get interested in Honeypot and system programming, so I'll write again when each result comes out.
Justin Seitz (2014) "Cyber Security Programming" Kazushi Aoki, Yu Arai, Saya Ichinose, Makoto Iwamura, Yuhei Kawakotani, Yuji Hoshizawa
Recommended Posts