Hello !
I have some class for getting html documents :
"""
Wrapper for Python sockets lib
"""
import socket
import urlparse
import random
import io
import re
import sys
# socket wrapper class
class sock:
def __init__(self,url):
parse = urlparse.urlparse(url)
self.req = [] # request tuple
self.response = "" # response data
self.port = socket.getservbyname("www","tcp") # remote host
port
if parse[2] is not '':
if parse[4] is not '':
self.path = parse[2] + "?" + parse[4]
else:
self.path = parse[2]
else:
self.path = "/" # request path
if parse[1] is not '':
self.host = parse[1] # remote host name
else:
self.host = ""
self.req.append("GET " + self.path + " HTTP/1.1")
self.req.append("Host: " + self.host)
# set user-agent
def useragent(self, useragent):
self.req.append("User-Agent: " + useragent)
# set document max size in bytes
def range(self, size=0):
self.range = size
# get response document body
def get_body(self):
body = self.response.split("\r\n\r\n", 2)
try:
return body[1]
except:
return self.response
# do http request
def request(self, timeout=60,chunk=1024):
self.req.append("Accept: */*")
self.req.append("Pragma: no-cache")
self.req.append("Connection: close")
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(timeout)
try:
s.connect((self.host,self.port))
except:
print "Cant connect to remote host: "+self.host
try:
s.sendall("\r\n".join(self.req)+"\r\n\r\n")
except:
print "Cant write data to socket"
while 1:
try:
buffer = s.recv(chunk)
except:
print "Cant read data from socket."
break
if not buffer :
break
self.response = self.response+buffer
if len(self.response) self.range and self.range != 0:
print "Document is too big"
break
try:
s.close()
except:
print "Cant close socket"
if __name__ == '__main__':
if len(sys.argv) < 2:
print '\nNo URL specified for module test.\nUsage: sock.py
<URL>'
sys.exit()
test = sock(sys.argv[1])
test.useragent("Mozilla/4.0 (compatible; MSIE 5.5; Windows NT
4.0)")
test.range()
test.request()
print test.get_body()
-----------
So, lets do :
sock.py "http://forums.childrenwithdiabetes.com/showthread.php?t=5030"
- it not ok , only some part of document.
wget "http://forums.childrenwithdiabetes.com/showthread.php?t=5030" -
it ok !
sock.py "http://www.google.com/" - it ok !
Why i got only some part of document ? This is some bug in sockets
module or i do something wrong in my code?
Help me please , iam "googled" several hours , but not found any
related information.
All my bests.Igor. 1 5079
xreload wrote:
Hello !
So, lets do :
sock.py "http://forums.childrenwithdiabetes.com/showthread.php?t=5030"
- it not ok , only some part of document.
wget "http://forums.childrenwithdiabetes.com/showthread.php?t=5030" -
it ok !
sock.py "http://www.google.com/" - it ok !
Why i got only some part of document ? This is some bug in sockets
module or i do something wrong in my code?
You have a bug in your code:
def get_body(self):
body = self.response.split("\r\n\r\n", 2)
try:
return body[1]
except:
return self.response
The split is breaking up the response in 2 splits, that is, three parts.
You meant to have it split up in TWO parts. So change the argument 2
to the split call, to 1.
Also, why not just use httplib/urllib etc? Just curious.
--Irmen This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: flupke |
last post by:
Hi,
I have a gui (made in wxPython) that enables a user to connect to a server
and issue some commands. The problem occurs when i try to disconnect the
client. It exits but it doesn't return to...
|
by: priya |
last post by:
Hi All,
Currently I am working in Socket Progrm written in C.
Here i pasted Both Server and client code..
Server.c code
|
by: Tony Caduto |
last post by:
Hi,
I am developing a socket server in C# and I want it to be blocking.
Anyway I have it working with threads, as each client connects the socket
spawned from the accept method is sent to a client...
|
by: zaebos |
last post by:
hi, i have this code which is part of a main program, to email from
within the program a log file:
int MailIt (char *mailserver, char *emailto, char *emailfrom,
char *emailsubject, char...
|
by: nephish |
last post by:
lo there,
i have a simple app that connects to a socket to get info from a server
i looks like this
serverhost = 'xxx.xxx.xxx.xxx'
serverport = 9520
aeris_sockobj =...
|
by: SpreadTooThin |
last post by:
client:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("192.168.1.101", 8080))
print 'Connected'
s.send('ABCD')
buffer = s.recv(4)
print buffer
s.send('exit')
|
by: s_4 |
last post by:
Hello!
Sorry about my english - I write to you from poland.
I have strange problem. Bind() return '16'. Documentation say that it
should return 0 (no error) or -1 (some error).
Here is the code...
|
by: Hendrik van Rooyen |
last post by:
While doing a netstring implementation I noticed that if you
build a record up using socket's recv(1), then when you close
the remote end down, the recv(1) hangs, despite having a short
time out...
|
by: sleddd |
last post by:
Can anyone explain why socket performance (throughput) varies
depending on the amount of data send and recv are called with?
For example: try creating a local client/server (running on the same...
|
by: Jean-Paul Calderone |
last post by:
On Mon, 12 May 2008 08:34:07 -0700 (PDT), petr.poupa@gmail.com wrote:
You cannot reconnect a socket. You need to create a new one for each
connection. It's also almost certainly the case that...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |