📄 socketmodule.c
字号:
if (newfd < 0)
#endif
return PySocket_Err();
/* Create the new object with unspecified family,
to avoid calls to bind() etc. on it. */
sock = (PyObject *) PySocketSock_New(newfd,
s->sock_family,
s->sock_type,
s->sock_proto);
if (sock == NULL) {
SOCKETCLOSE(newfd);
goto finally;
}
addr = makesockaddr(s->sock_fd, (struct sockaddr *)addrbuf,
addrlen);
if (addr == NULL)
goto finally;
res = Py_BuildValue("OO", sock, addr);
finally:
Py_XDECREF(sock);
Py_XDECREF(addr);
return res;
}
static char accept_doc[] =
"accept() -> (socket object, address info)\n\
\n\
Wait for an incoming connection. Return a new socket representing the\n\
connection, and the address of the client. For IP sockets, the address\n\
info is a pair (hostaddr, port).";
/* s.setblocking(1 | 0) method */
#ifndef SYMBIAN
static PyObject *
PySocketSock_setblocking(PySocketSockObject *s, PyObject *arg)
{
int block;
#ifndef RISCOS
#ifndef MS_WINDOWS
int delay_flag;
#endif
#endif
block = PyInt_AsLong(arg);
if (block == -1 && PyErr_Occurred())
return NULL;
Py_BEGIN_ALLOW_THREADS
#ifdef __BEOS__
block = !block;
setsockopt( s->sock_fd, SOL_SOCKET, SO_NONBLOCK,
(void *)(&block), sizeof( int ) );
#else
#ifndef RISCOS
#ifndef MS_WINDOWS
#ifdef PYOS_OS2
block = !block;
ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block));
#else /* !PYOS_OS2 */
delay_flag = fcntl (s->sock_fd, F_GETFL, 0);
if (block)
delay_flag &= (~O_NDELAY);
else
delay_flag |= O_NDELAY;
fcntl (s->sock_fd, F_SETFL, delay_flag);
#endif /* !PYOS_OS2 */
#else /* MS_WINDOWS */
block = !block;
ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);
#endif /* MS_WINDOWS */
#endif /* __BEOS__ */
#endif /* RISCOS */
Py_END_ALLOW_THREADS
Py_INCREF(Py_None);
return Py_None;
}
static char setblocking_doc[] =
"setblocking(flag)\n\
\n\
Set the socket to blocking (flag is true) or non-blocking (false).\n\
This uses the FIONBIO ioctl with the O_NDELAY flag.";
#endif //symbian
#ifdef RISCOS
/* s.sleeptaskw(1 | 0) method */
static PyObject *
PySocketSock_sleeptaskw(PySocketSockObject *s,PyObject *args)
{
int block;
int delay_flag;
if (!PyArg_GetInt(args, &block))
return NULL;
Py_BEGIN_ALLOW_THREADS
socketioctl(s->sock_fd, 0x80046679, (u_long*)&block);
Py_END_ALLOW_THREADS
Py_INCREF(Py_None);
return Py_None;
}
static char sleeptaskw_doc[] =
"sleeptaskw(flag)\n\
\n\
Allow sleeps in taskwindows.";
#endif
/* s.setsockopt() method.
With an integer third argument, sets an integer option.
With a string third argument, sets an option from a buffer;
use optional built-in module 'struct' to encode the string. */
static PyObject *
PySocketSock_setsockopt(PySocketSockObject *s, PyObject *args)
{
int level;
int optname;
int res;
char *buf;
int buflen;
int flag;
if (PyArg_ParseTuple(args, "iii:setsockopt",
&level, &optname, &flag)) {
buf = (char *) &flag;
buflen = sizeof flag;
}
else {
PyErr_Clear();
if (!PyArg_ParseTuple(args, "iis#:setsockopt",
&level, &optname, &buf, &buflen))
return NULL;
}
res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen);
if (res < 0)
return PySocket_Err();
Py_INCREF(Py_None);
return Py_None;
}
static char setsockopt_doc[] =
"setsockopt(level, option, value)\n\
\n\
Set a socket option. See the Unix manual for level and option.\n\
The value argument can either be an integer or a string.";
/* s.getsockopt() method.
With two arguments, retrieves an integer option.
With a third integer argument, retrieves a string buffer of that size;
use optional built-in module 'struct' to decode the string. */
static PyObject *
PySocketSock_getsockopt(PySocketSockObject *s, PyObject *args)
{
int level;
int optname;
int res;
PyObject *buf;
socklen_t buflen = 0;
#ifdef __BEOS__
/* We have incomplete socket support. */
PyErr_SetString(PySocket_Error, "getsockopt not supported");
return NULL;
#else
if (!PyArg_ParseTuple(args, "ii|i:getsockopt",
&level, &optname, &buflen))
return NULL;
if (buflen == 0) {
int flag = 0;
socklen_t flagsize = sizeof flag;
res = getsockopt(s->sock_fd, level, optname,
(void *)&flag, &flagsize);
if (res < 0)
return PySocket_Err();
return PyInt_FromLong(flag);
}
if (buflen <= 0 || buflen > 1024) {
PyErr_SetString(PySocket_Error,
"getsockopt buflen out of range");
return NULL;
}
buf = PyString_FromStringAndSize((char *)NULL, buflen);
if (buf == NULL)
return NULL;
res = getsockopt(s->sock_fd, level, optname,
(void *)PyString_AS_STRING(buf), &buflen);
if (res < 0) {
Py_DECREF(buf);
return PySocket_Err();
}
_PyString_Resize(&buf, buflen);
return buf;
#endif /* __BEOS__ */
}
static char getsockopt_doc[] =
"getsockopt(level, option[, buffersize]) -> value\n\
\n\
Get a socket option. See the Unix manual for level and option.\n\
If a nonzero buffersize argument is given, the return value is a\n\
string of that length; otherwise it is an integer.";
/* s.bind(sockaddr) method */
static PyObject *
PySocketSock_bind(PySocketSockObject *s, PyObject *addro)
{
struct sockaddr *addr;
int addrlen;
int res;
if (!getsockaddrarg(s, addro, &addr, &addrlen))
return NULL;
Py_BEGIN_ALLOW_THREADS
res = bind(s->sock_fd, addr, addrlen);
Py_END_ALLOW_THREADS
if (res < 0)
return PySocket_Err();
Py_INCREF(Py_None);
return Py_None;
}
static char bind_doc[] =
"bind(address)\n\
\n\
Bind the socket to a local address. For IP sockets, the address is a\n\
pair (host, port); the host must refer to the local host. For raw packet\n\
sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])";
/* s.close() method.
Set the file descriptor to -1 so operations tried subsequently
will surely fail. */
static PyObject *
PySocketSock_close(PySocketSockObject *s)
{
SOCKET_T fd;
if ((fd = s->sock_fd) != -1) {
s->sock_fd = -1;
Py_BEGIN_ALLOW_THREADS
(void) SOCKETCLOSE(fd);
Py_END_ALLOW_THREADS
}
Py_INCREF(Py_None);
return Py_None;
}
static char close_doc[] =
"close()\n\
\n\
Close the socket. It cannot be used after this call.";
/* s.connect(sockaddr) method */
static PyObject *
PySocketSock_connect(PySocketSockObject *s, PyObject *addro)
{
struct sockaddr *addr;
int addrlen;
int res;
if (!getsockaddrarg(s, addro, &addr, &addrlen))
return NULL;
Py_BEGIN_ALLOW_THREADS
res = connect(s->sock_fd, addr, addrlen);
Py_END_ALLOW_THREADS
if (res < 0)
return PySocket_Err();
Py_INCREF(Py_None);
return Py_None;
}
static char connect_doc[] =
"connect(address)\n\
\n\
Connect the socket to a remote address. For IP sockets, the address\n\
is a pair (host, port).";
/* s.connect_ex(sockaddr) method */
static PyObject *
PySocketSock_connect_ex(PySocketSockObject *s, PyObject *addro)
{
struct sockaddr *addr;
int addrlen;
int res;
if (!getsockaddrarg(s, addro, &addr, &addrlen))
return NULL;
Py_BEGIN_ALLOW_THREADS
res = connect(s->sock_fd, addr, addrlen);
Py_END_ALLOW_THREADS
if (res != 0) {
#ifdef MS_WINDOWS
res = WSAGetLastError();
#else
res = errno;
#endif
}
return PyInt_FromLong((long) res);
}
static char connect_ex_doc[] =
"connect_ex(address)\n\
\n\
This is like connect(address), but returns an error code (the errno value)\n\
instead of raising an exception when an error occurs.";
/* s.fileno() method */
static PyObject *
PySocketSock_fileno(PySocketSockObject *s)
{
#if SIZEOF_SOCKET_T <= SIZEOF_LONG
return PyInt_FromLong((long) s->sock_fd);
#else
return PyLong_FromLongLong((LONG_LONG)s->sock_fd);
#endif
}
static char fileno_doc[] =
"fileno() -> integer\n\
\n\
Return the integer file descriptor of the socket.";
#ifndef NO_DUP
/* s.dup() method */
static PyObject *
PySocketSock_dup(PySocketSockObject *s)
{
SOCKET_T newfd;
PyObject *sock;
newfd = dup(s->sock_fd);
if (newfd < 0)
return PySocket_Err();
sock = (PyObject *) PySocketSock_New(newfd,
s->sock_family,
s->sock_type,
s->sock_proto);
if (sock == NULL)
SOCKETCLOSE(newfd);
return sock;
}
static char dup_doc[] =
"dup() -> socket object\n\
\n\
Return a new socket object connected to the same system resource.";
#endif
/* s.getsockname() method */
static PyObject *
PySocketSock_getsockname(PySocketSockObject *s)
{
char addrbuf[256];
int res;
socklen_t addrlen;
if (!getsockaddrlen(s, &addrlen))
return NULL;
memset(addrbuf, 0, addrlen);
Py_BEGIN_ALLOW_THREADS
res = getsockname(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Py_END_ALLOW_THREADS
if (res < 0)
return PySocket_Err();
return makesockaddr(s->sock_fd, (struct sockaddr *) addrbuf, addrlen);
}
static char getsockname_doc[] =
"getsockname() -> address info\n\
\n\
Return the address of the local endpoint. For IP sockets, the address\n\
info is a pair (hostaddr, port).";
#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
/* s.getpeername() method */
static PyObject *
PySocketSock_getpeername(PySocketSockObject *s)
{
char addrbuf[256];
int res;
socklen_t addrlen;
if (!getsockaddrlen(s, &addrlen))
return NULL;
memset(addrbuf, 0, addrlen);
Py_BEGIN_ALLOW_THREADS
res = getpeername(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Py_END_ALLOW_THREADS
if (res < 0)
return PySocket_Err();
return makesockaddr(s->sock_fd, (struct sockaddr *) addrbuf, addrlen);
}
static char getpeername_doc[] =
"getpeername() -> address info\n\
\n\
Return the address of the remote endpoint. For IP sockets, the address\n\
info is a pair (hostaddr, port).";
#endif /* HAVE_GETPEERNAME */
/* s.listen(n) method */
static PyObject *
PySocketSock_listen(PySocketSockObject *s, PyObject *arg)
{
int backlog;
int res;
backlog = PyInt_AsLong(arg);
if (backlog == -1 && PyErr_Occurred())
return NULL;
Py_BEGIN_ALLOW_THREADS
if (backlog < 1)
backlog = 1;
res = listen(s->sock_fd, backlog);
Py_END_ALLOW_THREADS
if (res < 0)
return PySocket_Err();
Py_INCREF(Py_None);
return Py_None;
}
static char listen_doc[] =
"listen(backlog)\n\
\n\
Enable a server to accept connections. The backlog argument must be at\n\
least 1; it specifies the number of unaccepted connection that the system\n\
will allow before refusing new connections.";
#ifndef NO_DUP
/* s.makefile(mode) method.
Create a new open file object referring to a dupped version of
the socket's file descriptor. (The dup() call is necessary so
that the open file and socket objects may be closed independent
of each other.)
The mode argument specifies 'r' or 'w' passed to fdopen(). */
static PyObject *
PySocketSock_makefile(PySocketSockObject *s, PyObject *args)
{
extern int fclose(FILE *);
char *mode = "r";
int bufsize = -1;
#ifdef MS_WIN32
Py_intptr_t fd;
#else
int fd;
#endif
FILE *fp;
PyObject *f;
if (!PyArg_ParseTuple(args, "|si:makefile", &mode, &bufsize))
return NULL;
#ifdef MS_WIN32
if (((fd = _open_osfhandle(s->sock_fd, _O_BINARY)) < 0) ||
((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL))
#else
if ((fd = dup(s->sock_fd)) < 0 || (fp = fdopen(fd, mode)) == NULL)
#endif
{
if (fd >= 0)
SOCKETCLOSE(fd);
return PySocket_Err();
}
#ifdef USE_GUSI2
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -