- #!/usr/bin/env python
- import socket
- import sys
- HOST = 'coding.debuntu.org'
- GET = '/rss.xml'
- PORT = 80
- try:
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- except socket.error, msg:
- sys.stderr.write("[ERROR] %s\n" % msg[1])
- try:
- sock.connect((HOST, PORT))
- except socket.error, msg:
- sys.exit(2)
- sock.send("GET %s HTTP/1.0\r\nHost: %s\r\n\r\n" % (GET, HOST))
- string = ""
- while len(data):
- string = string + data
- data = sock.recv(1024)
- print string
- sys.exit(0)
Executing this script will produce:
./socket_client.py HTTP/1.1 200 OK Date: Thu, 05 Jun 2008 19:22:34 GMT Server: Apache Set-Cookie: SESS25b075688450425d14146275d5eee6c2=30f08bf3bc69bfc905708aa7e4321c6b; expires=Sat, 28 Jun 2008 22:55:54 GMT; path=/; domain=.coding.debuntu.org Expires: Sun, 19 Nov 1978 05:00:00 GMT Last-Modified: Thu, 05 Jun 2008 19:22:34 GMT Cache-Control: store, no-cache, must-revalidate Cache-Control: post-check=0, pre-check=0 Connection: close Content-Type: application/rss+xml; charset=utf-8 rss data.......
출처 - http://coding.debuntu.org/python-socket-simple-tcp-client
'Development > python' 카테고리의 다른 글
python - How do I refer to the null object in Python? (0) | 2015.06.27 |
---|---|
python - setuptool + MySQL-python 설치 (0) | 2012.05.15 |