Skip to content

Instantly share code, notes, and snippets.

@altnight
Created November 2, 2013 10:02
Show Gist options
  • Save altnight/7277377 to your computer and use it in GitHub Desktop.
Save altnight/7277377 to your computer and use it in GitHub Desktop.
ソケットからやりなおす まずは公式から http://docs.python.jp/2.7/library/socket.html#socket-example
#!/usr/bin/env python
# coding: utf-8
import sys
import socket
HOST = 'localhost'
PORT = 50007
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.send(sys.argv[1])
data = s.recv(1024)
s.close()
print 'Received', repr(data)
# Received 'hello'
# http://docs.python.jp/2.7/library/socket.html#socket-example
#!/usr/bin/env python
# coding: utf-8
import socket
HOST = 'localhost'
PORT = 50007
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connect conn', conn
# Connect conn <socket._socketobject object at 0x123456abc>
print 'Connected by', addr
# Connected by ('127.0.0.1', 57111)
# ...
# Connected by ('127.0.0.1', 57185)
while 1:
data = conn.recv(1024)
if not data:
break
conn.send(data)
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment