본문 바로가기

Research/Web

python socket관련 - socket, urllib2, httplib

티스토리는 여긴 퍼오기 옵션 같은게 없나...

출처 http://bbolmin.tistory.com/trackback/118

socket option header 지정 관련






[ socket ]



1
2
3
4
5
6
7
8
9
10
11
12
from socket import socket, AF_INET, SOCK_STREAM
 
host = '192.168.0.10'
port = 80
 
request = 'data'
 
s = socket(AF_INET, SOCK_STREAM)
s.connect((host, port))
s.send(request)
response = s.recv(1000)
s.close()






[ urllib2 ] 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import urllib
import urllib2
  
 
 
#########    GET    #########
req = urllib2.Request(url)
 
#########    POST   #########
data = {'id':'TestID', 'pw':'TestPW'}
data = urllib.urlencode(data)
req = urllib2.Request(url, data)
 
######## Header data #######
user_agent = 'test agent'
cookie = 'abc'
req.add_header('User-agent', user_agent)
req.add_header('cookie', cookie)
 
####### Proxy Setting #######
proxy = urllib2.ProxyHandler({'http': '127.0.0.1:8080'})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
 
 
#############################
response = urllib2.urlopen(req)
header = response.headers.headers
data = response.read()


User-agent 참고 : http://www.useragentstring.com/pages/useragentstring.php

Proxy-list site 참고 : http://gatherproxy.com/






[ httplib ]



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import httplib
import urllib
 
 
url = 'www.test.com'
conn = httplib.HTTPConnection(url)
 
#########    GET    #########
conn.request('GET', '/index.html')
 
#########    POST   #########
post_param = urllib.urlencode({'id':'test', 'pw':'test'})
headers = {'Cookie':'abcd'}
conn.request('POST', '/index.html', post_param, headers)
 
#############################
response = conn.getresponse()
 
print response.status
print response.reason
print response.getheaders()
print response.read()




urllib의 경우에는 user-agent 값이 디폴트로  "User-Agent: Python-urllib/1.17"가 되고 httplib는 따로 세팅해주지 않으면 존재하지 않는다.






추가


클라이언트

s = socket(AF_INET, SOCK_STREAM)

s.connect(('localhost', 9000))

s.send('HI')

s.close()


서버

from socket import *

s = socket(AF_INET, SOCK_STREAM)

s.bind(('localhost', 9000'))

s.listen(1)

conn, addr = s.accept()

...addr 에는 IP/PORT 정보 

conn.recv(1024)

conn.close()





https://wiki.python.org/moin/UdpCommunication

'Research > Web' 카테고리의 다른 글

IDS 우회 참고자료  (0) 2014.04.08
snort - Packet Acquisition  (0) 2014.02.14
Suricata 소스 특징정리  (0) 2014.01.13
[ Network ] IPv6.h // ndp.h  (0) 2013.09.10
htonl, htohl for 64bit  (0) 2013.08.26