Socket communication with Python

socket communication

1. Make a socket

2. Make a connection (TCP only)

3. Send and receive

Example: TCP communication

Get google page with http get

tcp.py


import socket

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(("www.google.co.jp",80))
client.send(b"GET / HTTP/1.1\r\nHost: google.com\r\n\r\n")
response = client.recv(4096)
print(response)

Example: UDP communication

Send "12345" to localserver.

udp-client.py


import socket

client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client.sendto(b"12345",("127.0.0.1",80))

data, addr = client.recvfrom(4096)
print(data)

Example: TCP server

Listen on port 5000 and return the current time

tcp-server.py


import socket
from datetime import datetime
from time import sleep

s = socket.socket()

port = 5000
s.bind(('', port))

while True:
    print('listening')
    s.listen(5)
    c, addr = s.accept()
    print('receiving')
    print(c.recv(4096))
    while True:
        print('sending')
        now = datetime.now().strftime("%Y/%m/%d %H:%M:%S")
        try:
            c.send(now)
        except:
            break
        sleep(1)
    c.close()
s.close()

tcp-client.py


import socket
from contextlib import closing
import sys

s = socket.socket()

host = sys.argv[1]
port = 5000

#with closing(s):
s.connect((host, port))
s.send("hi")
while True:
    print host, s.recv(4096)

Recommended Posts

Socket communication with Python
Socket communication with Python LEGO Mindstorms
Serial communication with python
HTTP communication with Python
Socket communication using socketserver with python now
View Python communication with Fiddler
Python3 socket module and socket communication flow
I tried SMTP communication with Python
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Socket communication and multi-thread processing by Python
Socket communication by C language and Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
Run prepDE.py with python3
Communication processing by Python
I tried to communicate with a remote server by Socket communication with Python.
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
Posting tweets with python
Drive WebDriver with python
Use mecab with Python3
[Python] Redirect with CGIHTTPServer
Voice analysis with python
Think yaml with python
Operate Kinesis with Python
Getting Started with Python